# CS3/AS2 - element moving (randomly) past stage borders

**URL:** <https://forum.kirupa.com/t/cs3-as2-element-moving-randomly-past-stage-borders/290233>\
**Category:** Uncategorized\
**Created:** [June 11, 2009, 2:38pm UTC](https://forum.kirupa.com/t/cs3-as2-element-moving-randomly-past-stage-borders/290233 "2009-06-11T14:38:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![swaddock](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@swaddock](https://forum.kirupa.com/u/swaddock)\
**Post date:** [June 11, 2009, 2:38pm UTC](https://forum.kirupa.com/t/cs3-as2-element-moving-randomly-past-stage-borders/290233/1 "2009-06-11T14:38:51Z")

</div>

New here…Using the Random tutorial i created a small swf of stars randomly moving about the stage. Problem is, the stars are moving past the right and bottom borders.

I don’t understand the AS well enough to know how to alter/limit the stars movement. What I’m looking for them to do is, appear to bounce off the stage borders.

Each star (movie clip) is 199x190

Any help would be greatly appreciated.

Here is Action script for each individual star:

```auto
 
onClipEvent (load) { 
//data you may want to change 
width = 800; 
height = 600; 
speed = Math.round(Math.random()*2)+2; 
//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) { 
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 = 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 = Math.random()*height; 
} 
}

```
