# Why wont this script work as i want to?

**URL:** <https://forum.kirupa.com/t/why-wont-this-script-work-as-i-want-to/218326>\
**Category:** flash\
**Created:** [March 6, 2007, 8:13pm UTC](https://forum.kirupa.com/t/why-wont-this-script-work-as-i-want-to/218326 "2007-03-06T20:13:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Macsy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/macsy/32/3238_2.png) [@Macsy](https://forum.kirupa.com/u/Macsy)\
**Post date:** [March 6, 2007, 8:13pm UTC](https://forum.kirupa.com/t/why-wont-this-script-work-as-i-want-to/218326/1 "2007-03-06T20:13:59Z")

</div>

Hi. I have a MC linked with AS called char. And the problem is this, the char is supose to walk towards the mouse when i click the mouse. But now the problem is that the MC never stops. It just walks and often in wrong direction.  
plz check it out and help me.

var speed = 5;  
var aim = “crsSword”;  
var Move = false;  
attachMovie(“char”, “char”, this.getNextHighestDepth());  
attachMovie(aim, “mouseAim”, this.getNextHighestDepth());  
movement = function (Move, target, dirX, dirY) {  
a = Math.atan2(dirY, dirX);  
if (Math.sqrt(Math.pow(dirX, 2)+Math.pow(dirY, 2))\>speed) {  
target.\_x += Math.cos(a)\*speed;  
target.\_y += Math.sin(a)\*speed;  
} else if (Math.abs(dirX)\>0 && Math.abs(dirY)\>0) {  
target.\_x = \_root.\_xmouse;  
target.\_y = \_root.\_ymouse;  
}  
};  
onEnterFrame = function () {  
mouseAim.\_x = \_root.\_xmouse;  
mouseAim.\_y = \_root.\_ymouse;  
movement(Move, char, \_root.\_xmouse, \_root.\_ymouse);  
};
