# Follow mouse, detect direction, distance to, play corresponding MC frame

**URL:** <https://forum.kirupa.com/t/follow-mouse-detect-direction-distance-to-play-corresponding-mc-frame/166225>\
**Category:** flash\
**Created:** [October 18, 2005, 7:06am UTC](https://forum.kirupa.com/t/follow-mouse-detect-direction-distance-to-play-corresponding-mc-frame/166225 "2005-10-18T07:06:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![parisJim](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@parisJim](https://forum.kirupa.com/u/parisJim)\
**Post date:** [October 18, 2005, 7:06am UTC](https://forum.kirupa.com/t/follow-mouse-detect-direction-distance-to-play-corresponding-mc-frame/166225/1 "2005-10-18T07:06:01Z")

</div>

Hello,  
Second time poster and a novice in actionscript.  
I can almost read it, just can’t seem to write it at all.

I’m trying to have the animation of a MC instance name “walking\_mc” follow the mouse on the x axis, arrive at the mouse and stop, turn then continue following the mouse in which ever direction it moves.  
here’s a link to the file:  
[http://jdittmer.free.fr/test/test.htm](http://jdittmer.free.fr/test/test.htm)  
[http://jdittmer.free.fr/test/walking.fla.zip](http://jdittmer.free.fr/test/walking.fla.zip)

here is what I have so far:

[AS]

function fMoveChar() {

```
    var xmouse = _root._xmouse;
    var xchar = myCharacter._x;
    
    if (xmouse &gt; xchar) var dir = 1;
    else var dir = -1
    
    myCharacter._x += myCharacter.speed * dir;
    
    var dist = Math.abs(xchar - xmouse);
    
    // use conditional statements and the dist variable to tell your mc to goto certain frames
	
	
	 if (dist &lt; 15) {
        
            myCharacter.gotoAndPlay("StopFromRight");

    
    } else myCharacter.speed = 4;
	
	  if (dist &lt; 2) {
            myCharacter.speed = 0;
            myCharacter._x = xmouse;
    
    } else myCharacter.speed = 4;

```

}

moveInt = setInterval(fMoveChar,44);

[AS]

I have an MC with the corresponding frames and the appropriate frame names ie; “LeftToRight”, “StopFromRight”, “LeaveToLeft”;  
I’m looking for the actionscript for that will allow it to:

- detect whether the \_xmouse is to the left or the right and play the corresponding frame.  
An if/else with the dist variable would give me part, but not direction.  
I’d appreciate any help you all could give.  
Thanks,  
JED
