# Detecting Direction of Mouse Movement

**URL:** https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815
**Category:** classic bestof
**Created:** [July 28, 2005, 6:19am UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815 "2005-07-28T06:19:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [July 28, 2005, 6:19am UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/1 "2005-07-28T06:19:20Z")

</div>

Hey everyone,  
I came up with two simple functions for determining the direction the mouse is moving, and I’m wondering if you all have an easier way of doing this:

```auto
vertical_check = function () {
 j = 0;
 v = [];
 this.createEmptyMovieClip("tempy", this.getNextHighestDepth());
 tempy.onMouseMove = function() {
  j++;
  v[j] = this._ymouse;
  if (j>1) {
   if (v[j]>v[j-1]) {
    trace("moving down");
    j = 0;
    v = [];
   } else {
    trace("moving up");
    j = 0;
    v = [];
   }
  }
 };
};
horizontal_check = function () {
 i = 0;
 h = [];
 this.createEmptyMovieClip("tempx", this.getNextHighestDepth());
 tempx.onMouseMove = function() {
  i++;
  h* = this._xmouse;
  if (i>1) {
   if (h*>h[i-1]) {
    trace("moving right");
    i = 0;
    h = [];
   } else {
    trace("moving left");
    i = 0;
    h = [];
   }
  }
 };
};
horizontal_check();
vertical_check();

```

This isn’t necessarily a bug fix, for my above code works. It just seems too complicated for something so simple. I’m planning on making this into a quick tutorial, so be warned that I may use your suggestions (with credit) in my writeup 🙂

Thanks!  
Kirupa :beam:

---

<div class="post-metadata">

### Author: ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)
#### Post date: [April 15, 2006, 11:44pm UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/2 "2006-04-15T23:44:36Z")

</div>

> [@nokrev](#):
>
> Oh really? :lol:

anyone know how to more accuratly determine mouse movement

like moving directly verticle would be 0  
moving dead right would be 90  
moving directly toward bottom would be 180  
moving dead left would be 270

or some way to get the exact trajectory?

---

<div class="post-metadata">

### Author: ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)
#### Post date: [April 16, 2006, 1:50am UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/3 "2006-04-16T01:50:53Z")

</div>

Try messing with the trigonometry. Most of these methods form a right triangle between the old and new positions, and then use a trigonometric function to find the angle. 🙂

---

<div class="post-metadata">

### Author: ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)
#### Post date: [April 16, 2006, 2:10am UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/4 "2006-04-16T02:10:16Z")

</div>

> [@nokrev](#):
>
> Try messing with the trigonometry. Most of these methods form a right triangle between the old and new positions, and then use a trigonometric function to find the angle. 🙂

heheh…ummm yeh i took a trig class in H.S. like 10 years ago…

---

<div class="post-metadata">

### Author: ![cobrasniper555](https://avatars.discourse-cdn.com/v4/letter/c/838e76/32.png) [@cobrasniper555](https://forum.kirupa.com/u/cobrasniper555)
#### Post date: [April 16, 2006, 2:48am UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/5 "2006-04-16T02:48:00Z")

</div>

Are you guys trying to just get something to point at the mouse???  
If so…it’s really easy, use trig! Here, I’ll make it easy for you.  
Now look at this:

```auto
var distx:Number = _root._xmouse-_x;//finds the x distance between the mouse and your movie clip
var disty:Number = _root._ymouse-_y;//finds the y distance between the mouse and your movie clip
var rad:Number = Math.atan2(disty,distx);//finds radians
_rotation = rad/(Math.PI/180);//Makes your movie clip rotate towards its target, the mouse

```

Hope I helped!

---

<div class="post-metadata">

### Author: ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)
#### Post date: [April 16, 2006, 2:51am UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/6 "2006-04-16T02:51:37Z")

</div>

> [@joran420](#):
>
> heheh…ummm yeh i took a trig class in H.S. like 10 years ago…

Then you should be good at it. 😛

[whisper]Just play with the functions until it works…[/whisper]

---

<div class="post-metadata">

### Author: ![belotte](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@belotte](https://forum.kirupa.com/u/belotte)
#### Post date: [July 1, 2008, 8:37pm UTC](https://forum.kirupa.com/t/detecting-direction-of-mouse-movement/156815/7 "2008-07-01T20:37:10Z")

</div>

Hello there Is there way th check left and right movement in a specific region?

Thanks
