Random Movement

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.

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

http://www.kirupa.com/developer/mx/random_motionMX.htm

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(aa+bb));
}
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]