Quantunet.com

My Account

Joins Us
Flash 8 Actionscript 2.0 QuickSkills
2D Random Motion Blodd Cells  


Download .fla

How to create this random motion.


Actionscript

accel = 3;

for(i=0;i<60;i++){
var b = this.attachMovie("blood","blood" +i,i);

b._x = Math.random()*Stage.width;
b._y = Math.random()*Stage.height;
b.randx = Math.round(Math.random()*Stage.width);
b.randy = Math.round(Math.random()*Stage.height);

b._xscale = Math.random ()*300+30;
b._yscale = b._xscale

b.onEnterFrame = function (){
this._x += (this.randx-this._x)/accel;
this._y += (this.randy-this._y)/accel;

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


  • Define variables.

  • Generate 60 red blood cells

  • 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 = 3;

for(i=0;i<60;i++){
var b = this.attachMovie("blood","blood" +i,i);

b._x = Math.random()*Stage.width;
b._y = Math.random()*Stage.height;
b.randx = Math.round(Math.random()*Stage.width);
b.randy = Math.round(Math.random()*Stage.height);

b._xscale = Math.random ()*300+30;
b._yscale = b._xscale

b.onEnterFrame = function (){
this._x += (this.randx-this._x)/accel;
this._y += (this.randy-this._y)/accel;

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

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