# Random Movement Tutorial

**URL:** <https://forum.kirupa.com/t/random-movement-tutorial/25686>\
**Category:** flash\
**Created:** [July 15, 2003, 6:07pm UTC](https://forum.kirupa.com/t/random-movement-tutorial/25686 "2003-07-15T18:07:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeoDreamer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@NeoDreamer](https://forum.kirupa.com/u/NeoDreamer)\
**Post date:** [July 15, 2003, 6:07pm UTC](https://forum.kirupa.com/t/random-movement-tutorial/25686/1 "2003-07-15T18:07:17Z")

</div>

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

This tutorial works for me when the random object moves on the main timeline, but when I apply the code to a movie clip inside another movie clip, it won’t work. I only see jumpy movement with no smooth tween between each jump.

---

<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 15, 2003, 7:33pm UTC](https://forum.kirupa.com/t/random-movement-tutorial/25686/2 "2003-07-15T19:33:36Z")

</div>

It doesn’t work because the functions are not defined in \_root anymore when you load the SWF into a MovieClip. Remove \_root from the code. 😉  
[AS]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));  
}  
MovieClip.prototype.reset = function() {  
width = 300;  
height = 200;  
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 = 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 (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;  
};[/AS]

---

<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 15, 2003, 8:53pm UTC](https://forum.kirupa.com/t/random-movement-tutorial/25686/3 "2003-07-15T20:53:04Z")

</div>

🙂 It worked! Thanks again!

---

<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 15, 2003, 8:55pm UTC](https://forum.kirupa.com/t/random-movement-tutorial/25686/4 "2003-07-15T20:55:51Z")

</div>

You’re welcome. =)
