1. Home
  2. Computing & Technology
  3. Animation

Creating Random Motion with Flash ActionScripts and Math.random

By Adrien-Luc Sanders, About.com

5 of 8

Now to add the randomizing function that changes the x position every time the movie enters the frame:

onClipEvent (enterFrame) {
this._x = this._x + Math.random() ;
}

To break this down simply, what this script does is tell Flash to check the x (horizontal) position in pixels of the symbol referenced by this, and then add the random number generated by Math.random to it so that it moves fractionally to the right.

Explore Animation
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Animation
  4. Flash Animation Tutorials
  5. Creating Random Motion with Flash ActionScripts and Math.random

©2009 About.com, a part of The New York Times Company.

All rights reserved.