# Simple Random Movement

**URL:** <https://forum.kirupa.com/t/simple-random-movement/112013>\
**Category:** Uncategorized\
**Created:** [June 2, 2004, 4:21pm UTC](https://forum.kirupa.com/t/simple-random-movement/112013 "2004-06-02T16:21:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 2, 2004, 4:21pm UTC](https://forum.kirupa.com/t/simple-random-movement/112013/1 "2004-06-02T16:21:49Z")

</div>

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:

[[size=4][color=red]http://www.kirupa.com/temp/zz2.html[/color][/size]](http://www.kirupa.com/temp/zz2.html)

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 😛

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:

```auto

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 🙂 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.

Cheers!  
Kirupa :ub:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2004, 4:31pm UTC](https://forum.kirupa.com/t/simple-random-movement/112013/2 "2004-07-07T16:31:31Z")

</div>

cool effect, I like it :thumb:

---

<div class="post-metadata">

**Author:** ![hardtosay](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hardtosay](https://forum.kirupa.com/u/hardtosay)\
**Post date:** [August 3, 2004, 4:09pm UTC](https://forum.kirupa.com/t/simple-random-movement/112013/3 "2004-08-03T16:09:55Z")

</div>

hello

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

---

<div class="post-metadata">

**Author:** ![innoxist](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@innoxist](https://forum.kirupa.com/u/innoxist)\
**Post date:** [August 9, 2004, 7:37am UTC](https://forum.kirupa.com/t/simple-random-movement/112013/4 "2004-08-09T07:37:21Z")

</div>

woot! thanks kirupa!
