Hi me again

This is with regards to Random motion

Can some one explain me the code explain me the code as if I am a 4year old---- please

thankyou

P.S. this is the random motion tutorial in kirupa’s tutorials:x

if you were a 4 year old I would tell you to get off of the computer and take a nap

Which part dont you get? I think i had problems with that before, but i might know some of it now…

The Random Motion tutorial is a pretty advanced code and its quite long. I don’t really have the time to explain everything but I can try to cut the functions up and give a brief explanation:


// First two functions
function getdistance (x, y, x1, y1) {
	var run, rise;
	run = x1-x;
	rise = y1-y;
	return (hyp(run, rise));
}
function hyp (a, b) {
	return (Math.sqrt(a*a+b*b));
}

These two functions just return the distance between two points. Its just math, simple Algebra.


MovieClip.prototype.reset = function () { 
var dist, norm, movie_height, movie_width;


//   movie_height: refers to the height of your movie
//   movie_width: refers to the width of your movie

//---------------------------------

movie_height = 150;
movie_width = 300;

//---------------------------------


speed = Math.random()*4+2;
targx = Math.random()*(movie_width-_width);
targy = Math.random()*(movie_height-_height);
dist = _root.getdistance(_x, _y, targx, targy);

norm = speed/dist;
diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;
};

This function is called reset because it resets the values. It gives the MovieClip a new X and Y target and determines the speed at which the MovieClip will move.

MovieClip.prototype.move = function () { 
var cycle;

// cycle: specifies the number of milliseconds waited 
// between movements

//--------------------------------------------

cycle = 200;

//--------------------------------------------

if (_root.getdistance(_x, _y, targx, targy)>speed) {
    x += diffx;
    y += diffy;
} else {
     x = targx;
     y = targy;
     if (!this.t) {
         t = getTimer();
    }
     if (getTimer()-t>cycle) {
         reset();
         t = 0;
     }
}
_x = x;
_y = y;
}

This function checks to find the position of the movieclip and if it is less than the variable “speed” it stops moving. Then the timer is called to tell the MovieClip when to move.

That just tells you what each Function does. For moer information go here:

Prototypes: http://www.kirupa.com/developer/actionscript/tricks/prototypes.asp

getTimer(): http://www.kirupaforum.com/forums/showthread.php?s=&threadid=16200&highlight=getTimer