A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [AS2] Random Npc Movement

  1. #1
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    [AS2] Random Npc Movement

    hi, I am trying to add random npc movement. I have created something but it's kind of a joke lol.

    Here is the code.

    Code:
    onClipEvent (load) {
    	npc01 = setInterval(function () {
    		ranmove();
    	}, 50);
    	i = 0;
    	function ranmove() {
    		if (dontrun != true) {
    			trace(ran1);
    			ran1 = Math.floor(Math.random()*6);
    			trace(ran1);
    			ran2 = Math.floor(Math.random()*100);
    			ran0 = Math.floor(Math.random()*25);
    			trace(ran2);
    			x2 = this._x-ran2;
    			y2 = this._y-ran2;
    			x3 = this._x+ran2;
    			y3 = this._y+ran2;
    			dontrun = true;
    		}
    		trace(ran1);
    		if (ran1 == 0) {
    			if (this._x<=x2) {
    				trace("Pick new 0");
    				dontrun = false;
    			} else {
    				trace("move 0");
    				this.gotoAndStop(8);
    				this._x -= 3;
    			}
    		}
    		if (ran1 == 1) {
    			if (this._y<=y2) {
    				trace("Pick new 1");
    				dontrun = false;
    			} else {
    				this.gotoAndStop(5);
    				trace("move 1");
    				this._y -= 3;
    			}
    		}
    		if (ran1 == 2) {
    			if (this._x>=x3) {
    				trace("Pick new 2");
    				dontrun = false;
    			} else {
    				trace("move 2");
    				this.gotoAndStop(6);
    				this._x += 3;
    			}
    		}
    		if (ran1 == 3) {
    			if (this._y>=y3) {
    				trace("Pick new 3");
    				dontrun = false;
    			} else {
    				trace("move 3");
    				this.gotoAndStop(7);
    				this._y += 3;
    			}
    		}
    		if (ran1 == 4 || ran1 == 5) {
    			if (i>=ran0 +8) {
    				trace("finish waiting");
    				i = 0;
    				ran2 = 0;
    				dontrun = false;
    				ran3 = Math.floor(Math.random()*4);
    			} else {
    				trace("waiting");
    				i++;
    				if (ran3 == 0) {
    					this.gotoAndStop(1);
    				}
    				if (ran3 == 1) {
    					this.gotoAndStop(2);
    				}
    				if (ran3 == 2) {
    					this.gotoAndStop(3);
    				}
    				if (ran3 == 3) {
    					this.gotoAndStop(4);
    				}
    			}
    		}
    	}
    }
    I think the npc appears to be on crack and is looking for a piece it may have dropped.

    any suggestions..

    http://www.swfcabin.com/open/1267070582

  2. #2
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    oh yea ignore what the player looks like and how he looks different at times, that's not important (is only like that for this test)

  3. #3
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    Code:
    onClipEvent (load) {
    	npc01 = setInterval(function () {
    		ranmove();
    	}, 50);
    	i = 0;
    	e = 0;
    	function ranmove() {
    		if (_level0.dontrun2 != true) {
    			trace(ran1);
    			ran1 = Math.floor(Math.random()*6);
    			trace(ran1);
    			ran2 = Math.floor(Math.random()*100);
    			ran5 = Math.floor(Math.random()*10);
    			ran0 = Math.floor(Math.random()*25);
    			trace(ran2);
    			x2 = this._x-ran2;
    			y2 = this._y-ran2;
    			x3 = this._x+ran2;
    			y3 = this._y+ran2;
    			_level0.dontrun = true;
    			_level0.dontrun2 = true;
    			e = 0;
    		}
    		trace(ran1);
    		if (ran1 == 0) {
    			if (this._x<=x2) {
    				trace("Pick new 0");
    				_level0.dontrun = false;
    				this.gotoAndStop(4);
    			} else {
    				trace("move 0");
    				this.gotoAndStop(8);
    				this._x -= 3;
    			}
    		}
    		if (ran1 == 1) {
    			if (this._y<=y2) {
    				trace("Pick new 1");
    				_level0.dontrun = false;
    				this.gotoAndStop(1);
    			} else {
    				this.gotoAndStop(5);
    				trace("move 1");
    				this._y -= 3;
    			}
    		}
    		if (ran1 == 2) {
    			if (this._x>=x3) {
    				trace("Pick new 2");
    				_level0.dontrun = false;
    				this.gotoAndStop(2);
    			} else {
    				trace("move 2");
    				this.gotoAndStop(6);
    				this._x += 3;
    			}
    		}
    		if (ran1 == 3) {
    			if (this._y>=y3) {
    				trace("Pick new 3");
    				_level0.dontrun = false;
    				this.gotoAndStop(3);
    			} else {
    				trace("move 3");
    				this.gotoAndStop(7);
    				this._y += 3;
    			}
    		}
    		if (ran1 == 4 || ran1 == 5) {
    			if (i>=ran0+8) {
    				trace("finish waiting");
    				i = 0;
    				ran2 = 0;
    				_level0.dontrun = false;
    				_level0.dontrun2 = false;
    				ran3 = Math.floor(Math.random()*4);
    			} else {
    				trace("waiting");
    				i++;
    				if (ran3 == 0) {
    					this.gotoAndStop(1);
    				}
    				if (ran3 == 1) {
    					this.gotoAndStop(2);
    				}
    				if (ran3 == 2) {
    					this.gotoAndStop(3);
    				}
    				if (ran3 == 3) {
    					this.gotoAndStop(4);
    				}
    			}
    		}
    		if (ran1<4) {
    			trace("Pick new but wait");
    			if (_level0.dontrun == false) {
    				trace("actually waiting");
    				ran1 = -1;
    				trace("e = "+e+" r = "+ran5);
    				if (e == ran5) {
    					trace("stop waiting");
    					_level0.dontrun2 = false;
    				}
    				e++;
    			}
    		}
    	}
    }
    also this is one of the proxies (bottom one)

    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_parent.npc.testhit)) {
    		_level0.dontrun = false;
    		_parent.npc._y -= 3;
    	}
    }
    he can walk through it for some reason..
    yet the sides have a similer code and he cant walk through the sides..

    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_parent.npc.testhit)) {
    		_level0.dontrun = false;
    		_parent.npc._x += 3;
    	}
    }
    recent swf
    http://www.swfcabin.com/open/1267072771
    im assuming this is too big for no reason. lool
    Last edited by brickhouse420; 02-25-2010 at 01:31 AM.

  4. #4
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    He is looking like he is on crack because he is making random moves every frame.

    Instead of this you want to have him change directions less often, and walk in a set direction until he changes his mind again.

    Store his x and y velocity somewhere and then every frame you have him continue in that direction. If you are using as2, you can just haphazardly add properties to the movieclip, so an easy way to do this would be to have a this.xspeed and this.yspeed that are randomly set to some small number.maxspeed);

    Every frame then you can do something like:

    Actionscript Code:
    this._x += this.xspeed;
    this._y += this.yspeed;

    and then you can call a subroutine on a timer that re-sets the xspeed and yspeed periodically. you can even make the timer that sets this random. (look into setInterval and setTimeout for this). One of them keeps calling itself, and the other is a onetime call. I like the onetime call for something like this, otherwise you would have to keep clearing it every time, either way. Something like:
    Actionscript Code:
    function changedirection(thismc){
    var maxspeed = 50;
    var mindelay = 500;
    var delayvariance = 2000;
    thismc.xspeed = maxspeed - (Math.random() * 2 * maxspeed);
    thismc.yspeed = maxspeed - (Math.random() * 2 * maxspeed);
    var delay = (Math.random() * delayvariance) + mindelay;
    setTimeout(changedirection, delay, thismc)
    }

    then somewhere in your movieclip creation you kick the recurring, randomly delayed function calls off with:
    changedirection(this);

    This would make him change his mind every 500 - 2500 milliseconds, or 0.5 to 2.5 seconds. If you need logic that keeps him from wandering out of a certain area, that would be put in the changedirection function. Helps you keep your enterframe code as simple as possible as well.


    [edit:]
    setTimeout was undocumented until flash CS3 I believe, but it for SURE works in flash 8 and I believe in flash MX as well. Read the online livedocs for as2 or just use the helpfile for setInterval. setTimeout is the same except it does not re-occur, and effectively clears itself every call. Most of the time when I want to set an interval I want to do it somewhat randomly like this, so I use it a LOT more than setInterval. If I want to do something regularly I usually just handle it a function call from my main enterframe. Just a personal preference there.
    Last edited by Alluvian; 02-25-2010 at 02:18 PM.

  5. #5
    Member
    Join Date
    Aug 2006
    Posts
    77
    I have an very advanced random npc movement in my game "ultimate assassin 2". I can't give you the source but I think seeing it will help you: http://www.games121.com/2009/06/ulti...ssassin-2.html There isn't any registration points or way points.

  6. #6
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    http://www.swfcabin.com/open/1267127302 this is my test. the code is updated from what is seen here. what do you think?

    this below url is with what I think a bit less frequent movements.

    http://www.swfcabin.com/open/1267127840
    Last edited by brickhouse420; 02-25-2010 at 04:50 PM.

  7. #7
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    deleted post
    Last edited by brickhouse420; 02-27-2010 at 11:55 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center