# Random Movement

**URL:** https://forum.kirupa.com/t/random-movement/26432
**Category:** flash
**Created:** [July 23, 2003, 2:21am UTC](https://forum.kirupa.com/t/random-movement/26432 "2003-07-23T02:21:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![poLka](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@poLka](https://forum.kirupa.com/u/poLka)
#### Post date: [July 23, 2003, 2:21am UTC](https://forum.kirupa.com/t/random-movement/26432/1 "2003-07-23T02:21:51Z")

</div>

[http://www.kirupaforum.com/forums/register.php?a=act&u=6558&i=78917564](http://www.kirupaforum.com/forums/register.php?a=act&u=6558&i=78917564)

hi all, i am new here and was wondering if someone would take the time to either point me to an explanation or explain the above sample code for Random Movement in Flash MX. Cheers.

---

<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 23, 2003, 3:15am UTC](https://forum.kirupa.com/t/random-movement/26432/2 "2003-07-23T03:15:54Z")

</div>

Uhhh, I think the link you supplied is wrong :x

---

<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 23, 2003, 3:35am UTC](https://forum.kirupa.com/t/random-movement/26432/3 "2003-07-23T03:35:09Z")

</div>

[http://www.kirupa.com/developer/mx/random\_motionMX.htm](http://www.kirupa.com/developer/mx/random_motionMX.htm)

---

<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 23, 2003, 4:12am UTC](https://forum.kirupa.com/t/random-movement/26432/4 "2003-07-23T04:12:19Z")

</div>

Well I am not going to get all deep and elaborate on it, but I will comment some things in the code…

If you don’t understand from that, I recommend starting lower… crawl before you walk, walk before you run.

[AS]function getdistance(x, y, x1, y1) {  
//define run and rise variables  
var run, rise;  
//get distance x  
run = x1-x;  
//get distance y  
rise = y1-y;  
//get hypotenuse of distance x and distance y  
return (\_root.hyp(run, rise));  
}  
function hyp(a, b) {  
//equation to get hypotenuse of x and y distances  
return (Math.sqrt(a_a+b_b));  
}  
MovieClip.prototype.reset = function() {  
//specify the width and height of the movie  
width = 300;  
height = 200;  
//-------------------  
var dist, norm;  
//define variables x and y in the clip and set them to the current \_x and \_y positions of the clip  
this.x = this.\_x;  
this.y = this.\_y;  
//set a variable speed in the clip and give it a random value of 2-6  
this.speed = Math.random()\*4+2;  
//set a variable to hold the end position on the \_x axis and have it be within the width of the movie  
this.targx = Math.random()\*width;  
//set a variable to hold the endposition on the \_y axis and have it be within the height of the movie  
this.targy = Math.random()\*height;  
//get the distance between the clips current point and the clips end points (targx and targy variables)  
dist = \_root.getdistance(this.x, this.y, this.targx, this.targy);  
//equations to move clip to new target area  
norm = this.speed/dist;  
this.diffx = (this.targx-this.x)\*norm;  
this.diffy = (this.targy-this.y)\*norm;  
};  
MovieClip.prototype.move = function() {  
//if the distance between the current clip position and the new clip position is greater than the value of the speed variable  
if (\_root.getdistance(this.x, this.y, this.targx, this.targy)\>this.speed) {  
//increment the x variable by the diffx variable to move the clip on the \_x axis  
this.x += this.diffx;  
//increment the y variable by the diffy variable to move the clip on the \_y axis  
this.y += this.diffy;  
} else {  
//else  
//set the x variable to be equal to the new target x position  
this.x = this.targx;  
//set the y variable to be equal to the new target y position  
this.y = this.targy;  
//if the variable t is false  
if (!this.t) {  
//set the variable t to be a timer  
this.t = getTimer();  
}  
//if the current time elapsed minus the t variable is greater than 1 second (1000 milliseconds)  
if (getTimer()-this.t\>1000) {  
//call the reset function  
this.reset();  
//set the t variable back to 0  
this.t = 0;  
}  
}  
//set the \_x position to the x variable  
this.\_x = this.x;  
//set the \_y position to the y variable  
this.\_y = this.y;  
};[/AS]
