Quantunet.com

My Account

Joins Us
Flash 8 Actionscript 2.0 Knowledgebase
Basic Random 1D Motion I  

Download

How to create this random motion.


Actionscript

accel = 4;
var randx = Math.round(Math.random()*Stage.width);

ball_mc.onEnterFrame = function (){
ball_mc._x += (randx-ball_mc._x)/accel;

if(Math.round(ball_mc._x)==randx){
randx = Math.round(Math.random()*Stage.width);
}
}


  • Define variables.

  • Generate random numbers for the x and y positions.

  • Gradually move the object to this position.

  • Tell object to to repeat the process when it reaches the new position.


accel = 4;
var randx = Math.round(Math.random()*Stage.width);

ball_mc.onEnterFrame = function (){
ball_mc._x += (randx-ball_mc._x)/accel;

if(Math.round(ball_mc._x)==randx){
randx = Math.round(Math.random()*Stage.width);
}
}


© 2008 Quantunet LLC All Rights Reserved | Intellectual Property | Terms of Use | Privacy
Home | About Quantunet | FAQ's | Contact Us