Simple Random Movement

Hey everyone,
I noticed that a lot of the random movement tutorials on this site and others used code that would scare many beginner flashers. At the expense of optimization, I tried to create a more simple, yet working, random movement animation using only simple ActionScript. A demo can be found here:


I avoided a lot of the more complicated AS procedures - partially because I did not want the upcoming tutorial to scare beginners, and partially because I don’t fully understand the complicated AS things :stuck_out_tongue:

The file size of the SWF is only about 640 bytes, and the 25 circles did not tax the CPU resources. The percentage of CPU power used was about 0% on my comp.

Here is the code I used. The code can be applied to any movieclip:

onClipEvent (load) {
 //data you may want to change
 width = 300;
 height = 200;
 speed = Math.round(Math.random()*2)+1;
 this._alpha = Math.round(Math.random()*80)+20;
 this._xscale = this._yscale=Math.round(Math.random()*80)+20;
 //initial positions
 x = this._x=Math.random()*width;
 y = this._y=Math.random()*height;
 x_new = Math.random()*width;
 y_new = Math.random()*height;
onClipEvent (enterFrame) {
 //x movement
 if (x_new>this._x) {
  dir_x = "positive";
  sign_x = 1;
 } else {
  dir_x = "negative";
  sign_x = -1;
 dx = Math.abs(x_new-this._x);
 if ((dx>speed) || (dx<-speed)) {
  this._x += sign_x*speed;
 } else {
  x_new = Math.random()*width;
 //y movement
 if (y_new>this._y) {
  dir_y = "positive";
  sign_y = 1;
 } else {
  dir_y = "negative";
  sign_y = -1;
 dy = Math.abs(y_new-this._y);
 if ((dy>speed) || (dy<-speed)) {
  this._y += sign_y*speed;
 } else {
  y_new = Math.random()*height;

The first frame contains a duplicate movie section of code that duplicates the above movie clip x number of times. You can search the site for examples of how that was done.

Hope you all like it :slight_smile: A tutorial on this should go up soon. I will probably go back and fix any of the logic errors that occur with some of the if statements and || determiners.

Kirupa :ub:

hello, i’m new here and don’t have experience with coding. but this random movement action script really helped me out. I do have a question though - when i alter the height/width of the movement, is there a way to specify where the movement can take place? For example, i have a 400 pixel height document, but i want the random objects moving in the lower portion of the document, but with a 250 pixel range. if i try changing the height to 250, i am only able to get the objects to move in the upper portion of the entire document.

any suggestions/hints/examples would be greaty appreciated!

thank you!

cool effect, I like it :thumb:

really hot effect
thanks man


this is a great script.
i have some question about this effect.

if i have a button inside the random movie clip.
i mean it’s just like a random moving button.
how to make the moving button stop when mouse over,
and keep moving when mouse roll out?

thanks very much

woot! thanks kirupa!