# Random Motion Boundary

**URL:** <https://forum.kirupa.com/t/random-motion-boundary/225668>\
**Category:** flash\
**Created:** [May 14, 2007, 7:55pm UTC](https://forum.kirupa.com/t/random-motion-boundary/225668 "2007-05-14T19:55:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bootyhunta](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@bootyhunta](https://forum.kirupa.com/u/bootyhunta)\
**Post date:** [May 14, 2007, 7:55pm UTC](https://forum.kirupa.com/t/random-motion-boundary/225668/1 "2007-05-14T19:55:16Z")

</div>

I am using the script here:  
[http://www.kirupa.com/developer/mx/random\_motionMX.htm](http://www.kirupa.com/developer/mx/random_motionMX.htm)

It works great. I just need to set a boundary for the items that is smaller than the actual size of the flash piece.

My flash piece is 300x250, but I need the objects to stay within a boundary that is between 10 - 290 on the x-axis and 35 - 195 on the y-axis.

Here is the code:

```auto
 
//special thanks to Suprabeener for the code
function getdistance(x, y, x1, y1) {
 var run, rise;
 run = x1-x;
 rise = y1-y;
 return (_root.hyp(run, rise));
}
function hyp(a, b) {
 return (Math.sqrt(a*a+b*b));
}
MovieClip.prototype.reset = function() {
 //specify the width and height of the movie
 width = 280;
 height = 160;
 //-------------------
 var dist, norm;
 this.x = this._x;
 this.y = this._y;
 this.speed = Math.random()*4+2;
 this.targx = Math.random()*width;
 this.targy = Math.random()*height;
 dist = _root.getdistance(this.x, this.y, this.targx, this.targy);
 norm = this.speed/dist;
 this.diffx = (this.targx-this.x)*norm;
 this.diffy = (this.targy-this.y)*norm;
};
MovieClip.prototype.move = function() {
 if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) {
  this.x += this.diffx;
  this.y += this.diffy;
 } else {
  this.x = this.targx;
  this.y = this.targy;
  if (!this.t) {
   this.t = getTimer();
  }
  if (getTimer()-this.t>1000) {
   this.reset();
   this.t = 0;
  }
 }
 this._x = this.x;
 this._y = this.y;
};

```

Any ideas?
