|
-
Senior Member
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|