# Random movement query

**URL:** <https://forum.kirupa.com/t/random-movement-query/144416>\
**Category:** Uncategorized\
**Created:** [April 14, 2005, 3:58am UTC](https://forum.kirupa.com/t/random-movement-query/144416 "2005-04-14T03:58:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@Bruce](https://forum.kirupa.com/u/Bruce)\
**Post date:** [April 14, 2005, 3:58am UTC](https://forum.kirupa.com/t/random-movement-query/144416/1 "2005-04-14T03:58:09Z")

</div>

Hi all,

I am trying to get a bee to fly randomly around the screen. I have searched the forum and found a tutorial that works - the bee does fly in all directions, except I want the bee to turn around and face the direction in which it flies each time. The script that I am using is below and would appreciate any help with inserting the necessary code. I have tried a number of codes but nothing works.

_Code for frame1_

//special thanks to Suprabeener for the code  
function getdistance(x, y, x1, y1) {  
var run, rise;  
run = x1-x;  
rise = y1-y;  
return (\_root.hyp(run, rise));  
}  
function hyp(a, b) {  
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;  
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 = \_root.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 (\_root.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;  
};

_Code for MC:_

onClipEvent(enterFrame){  
move();  
}

Thanks,  
B
