# Simple Random Movement

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

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 2, 2004, 4:21pm UTC](https://forum.kirupa.com/t/simple-random-movement/53329/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 6, 2004, 7:54pm UTC](https://forum.kirupa.com/t/simple-random-movement/53329/2 "2004-07-06T19:54:47Z")

</div>

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!

---

<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/53329/3 "2004-07-07T16:31:31Z")

</div>

cool effect, I like it :thumb:

---

<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 9, 2004, 1:53am UTC](https://forum.kirupa.com/t/simple-random-movement/53329/4 "2004-07-09T01:53:16Z")

</div>

really hot effect  
thanks man

---

<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:** [August 3, 2004, 4:09pm UTC](https://forum.kirupa.com/t/simple-random-movement/53329/5 "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:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 9, 2004, 7:37am UTC](https://forum.kirupa.com/t/simple-random-movement/53329/6 "2004-08-09T07:37:21Z")

</div>

woot! thanks kirupa!
