# Random Motion A/S not working on all instances

**URL:** https://forum.kirupa.com/t/random-motion-a-s-not-working-on-all-instances/269424
**Category:** flash
**Created:** [August 27, 2008, 1:03pm UTC](https://forum.kirupa.com/t/random-motion-a-s-not-working-on-all-instances/269424 "2008-08-27T13:03:59Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Maddug](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Maddug](https://forum.kirupa.com/u/Maddug)
#### Post date: [August 27, 2008, 1:03pm UTC](https://forum.kirupa.com/t/random-motion-a-s-not-working-on-all-instances/269424/1 "2008-08-27T13:03:59Z")

</div>

Hello,

I’m using this code found on this site for random motion. I have about 40 spheres that I’ve attached it to. The problem I’m having is I can never get all the spheres to move…usually one to three of them just sit still. And since it’s random it changes everytime I view that page. I need them all to move, does anyone have any suggestions?

Thank you.

```auto
 
onClipEvent (load) {
//data you may want to change
posx =478.9
posy =235.4
width = 5;
height = 5;
//speed = Math.round(Math.random()*2)+1;
speed = Math.random()*.35;
//initial positions
x = this._x=posx+Math.random()*width;
y = this._y=posy+Math.random()*height;
x_new = posx+Math.random()*width;
y_new =posy+ Math.random()*height;
}
onClipEvent (enterFrame) {
//x movement
if (x_new>this._x) {
sign_x = 1;
} else {
sign_x = -1;
}
dx = Math.abs(x_new-this._x);
if ((dx>speed) || (dx<-speed)) {
this._x += sign_x*speed;
} else {
x_new = posx+Math.random()*width;
}
//y movement
if (y_new>this._y) {
sign_y = 1;
} else {
sign_y = -1;
}
dy = Math.abs(y_new-this._y);
if ((dy>speed) || (dy<-speed)) {
this._y += sign_y*speed;
} else {
y_new =posy+Math.random()*height;
}
}

```
