# Need help with mouse follow and controling speed

**URL:** <https://forum.kirupa.com/t/need-help-with-mouse-follow-and-controling-speed/233753>\
**Category:** flash\
**Created:** [July 26, 2007, 3:59pm UTC](https://forum.kirupa.com/t/need-help-with-mouse-follow-and-controling-speed/233753 "2007-07-26T15:59:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omicam](https://avatars.discourse-cdn.com/v4/letter/o/b5e925/32.png) [@Omicam](https://forum.kirupa.com/u/Omicam)\
**Post date:** [July 26, 2007, 3:59pm UTC](https://forum.kirupa.com/t/need-help-with-mouse-follow-and-controling-speed/233753/1 "2007-07-26T15:59:06Z")

</div>

Hi,

I would need help with controling the speed like this:  
When the mouse is moving closer to the square then the speed increases.  
When the mouse is moving further from the square then the speed decreases.  
Basicly the mouse following is done already i just need to put that speed controling  
in place. Oh yeah and i have no clue why the square shakes when it hits the mouse.  
Here is the code:

init();  
function init():Void {  
speed = 10;  
var square = attachMovie(“square”, “square”, 0);  
square.\_x = Stage.width/2;  
square.\_y = Stage.height/2;  
}  
function onEnterFrame():Void {  
var dx:Number = \_xmouse-square.\_x;  
var dy:Number = \_ymouse-square.\_y;  
var angle:Number = Math.atan2(dy, dx);  
square.\_rotation = angle\*180/Math.PI;  
var vx:Number = Math.cos(angle)\*speed;  
var vy:Number = Math.sin(angle)\*speed;  
square.\_x += vx;  
square.\_y += vy;

}

Thanx a lot,  
Omicam
