# Hi me again

**URL:** <https://forum.kirupa.com/t/hi-me-again/18674>\
**Category:** flash\
**Created:** [April 19, 2003, 1:48pm UTC](https://forum.kirupa.com/t/hi-me-again/18674 "2003-04-19T13:48:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vdesigns](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@vdesigns](https://forum.kirupa.com/u/vdesigns)\
**Post date:** [April 19, 2003, 1:48pm UTC](https://forum.kirupa.com/t/hi-me-again/18674/1 "2003-04-19T13:48:40Z")

</div>

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

---

<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:** [April 19, 2003, 1:58pm UTC](https://forum.kirupa.com/t/hi-me-again/18674/2 "2003-04-19T13:58:46Z")

</div>

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

---

<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:** [April 19, 2003, 2:09pm UTC](https://forum.kirupa.com/t/hi-me-again/18674/3 "2003-04-19T14:09:51Z")

</div>

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

---

<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:** [April 19, 2003, 2:23pm UTC](https://forum.kirupa.com/t/hi-me-again/18674/4 "2003-04-19T14:23:04Z")

</div>

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:

```auto

// 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.

```auto

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.

```auto
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](http://www.kirupa.com/developer/actionscript/tricks/prototypes.asp)

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