# Keeping Randomly Placed Objects Within The Stage

**URL:** <https://forum.kirupa.com/t/keeping-randomly-placed-objects-within-the-stage/260759>\
**Category:** flash\
**Created:** [May 19, 2008, 5:19pm UTC](https://forum.kirupa.com/t/keeping-randomly-placed-objects-within-the-stage/260759 "2008-05-19T17:19:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsm\_](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jsm\_](https://forum.kirupa.com/u/jsm_)\
**Post date:** [May 19, 2008, 5:19pm UTC](https://forum.kirupa.com/t/keeping-randomly-placed-objects-within-the-stage/260759/1 "2008-05-19T17:19:53Z")

</div>

Hello,

I am placing 10 movie clips randomly within the stage using this simple script:

```auto
 
for(i=0; i<10; i++) {
 var t = this.attachMovie("imgs","imgs"+i,i);
 t.cacheAsBitmap = true;
 t._x = Stage.width*Math.random(550);
 t._y = Stage.height*Math.random(450);
 t.onRollOver = function(){
  this.swapDepths(this._parent.getNextHighestDepth());
 }
}

```

What I am trying to figure out is how to keep all the objects contained within the stage bondaries. Right now some of them are going off screen when they are placed, and this looks goofy when placed in the HTML page I am creating.

Any ideas on keeping them contained in an area so the edges are all within said area?

thanks,  
jsm\_

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 19, 2008, 6:03pm UTC](https://forum.kirupa.com/t/keeping-randomly-placed-objects-within-the-stage/260759/2 "2008-05-19T18:03:34Z")

</div>

Assuming the registration point for “imgs” is set to the upper-right corner:

```auto
 t._x = (Stage.width-t._width)*Math.random(Stage.width);
 t._y = (Stage.height-t._height)*Math.random(Stage.width);

```

---

<div class="post-metadata">

**Author:** ![jsm\_](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jsm\_](https://forum.kirupa.com/u/jsm_)\
**Post date:** [May 19, 2008, 7:29pm UTC](https://forum.kirupa.com/t/keeping-randomly-placed-objects-within-the-stage/260759/3 "2008-05-19T19:29:10Z")

</div>

Works perfect, and so simple… should have figured that out.

Thanks again.
