Animation

  1. Home
  2. Computing & Technology
  3. Animation

Creating Random Motion with Flash ActionScripts and Math.random

By Adrien-Luc Sanders, About.com

7 of 8

We’re only going to add one small thing:

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

What we’ve just done is very simple; we’ve taken the decimal number returned by Math.random and multiplied it by 100, so that now it’s greater than one and could be anything from 0 to 100.

This makes the random motion more timely, but now we run into another problem: the object dives off the stage pretty quickly, and doesn’t come back. In the next step, we’ll make some adjustments to the script that look complicated, but really aren’t.

7 of 8

Index: Creating Random Motion with Flash ActionScripts and Math.random

  1. Part 1
  2. Part 2
  3. Part 3
  4. Part 4
  5. Part 5
  6. Part 6
  7. Part 7
  8. Part 8

Explore Animation

More from About.com

Animation

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

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

All rights reserved.