# Detecting Mouse Direction

**URL:** <https://forum.kirupa.com/t/detecting-mouse-direction/185584>\
**Category:** flash\
**Created:** [April 14, 2006, 1:27pm UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584 "2006-04-14T13:27:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abstroose](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@abstroose](https://forum.kirupa.com/u/abstroose)\
**Post date:** [April 14, 2006, 1:27pm UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584/1 "2006-04-14T13:27:22Z")

</div>

Hey, I’m having a problem when detecting the direction of the mouse.

I’m using the following script:

```auto

[COLOR=#0000ff]stop[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#808080]*//*[/COLOR]
[COLOR=#000000] **var** [/COLOR] old_x:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]_xmouse[/COLOR];
[COLOR=#000000] **var** [/COLOR] old_y:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]_ymouse[/COLOR];
[COLOR=#808080]*//*[/COLOR]
[COLOR=#000000] **function** [/COLOR] getDir[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]:[COLOR=#0000ff]Void[/COLOR] [COLOR=#000000]{[/COLOR]
    [COLOR=#000000] **var** [/COLOR] new_x:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]_xmouse[/COLOR];
    [COLOR=#000000] **var** [/COLOR] new_y:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]_ymouse[/COLOR];
    new_x > old_x ? [COLOR=#0000ff]trace[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]"moving right"[/COLOR][COLOR=#000000])[/COLOR] : [COLOR=#0000ff]trace[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]"moving left"[/COLOR][COLOR=#000000])[/COLOR];
    new_y > old_y ? [COLOR=#0000ff]trace[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]"moving down"[/COLOR][COLOR=#000000])[/COLOR] : [COLOR=#0000ff]trace[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]"moving up"[/COLOR][COLOR=#000000])[/COLOR];
    old_x = new_x;
    old_y = new_y;
[COLOR=#000000]}[/COLOR]
[COLOR=#808080]*//*[/COLOR]
[COLOR=#0000ff]this[/COLOR].[COLOR=#0000ff]onMouseMove[/COLOR] = getDir;

```

But for some reason I’m not getting any output. Any help would be much appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![kBisk](https://avatars.discourse-cdn.com/v4/letter/k/eb8c5e/32.png) [@kBisk](https://forum.kirupa.com/u/kBisk)\
**Post date:** [April 14, 2006, 2:08pm UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584/2 "2006-04-14T14:08:47Z")

</div>

Tested the AS and it works just fine for me… just keep the mouse off of the output panel.

---

<div class="post-metadata">

**Author:** ![hybrid101](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@hybrid101](https://forum.kirupa.com/u/hybrid101)\
**Post date:** [April 15, 2006, 3:24am UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584/3 "2006-04-15T03:24:56Z")

</div>

it works fine for me too:)

---

<div class="post-metadata">

**Author:** ![abstroose](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@abstroose](https://forum.kirupa.com/u/abstroose)\
**Post date:** [April 15, 2006, 11:29am UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584/4 "2006-04-15T11:29:46Z")

</div>

Well I’m not sure what the problem was but yesterday I was using my uncles PC but now I am home the scripts works perfectly. However I am now having another problem. Here is the script I used on the first frame:

```auto
stop();
var old_x:Number = this._xmouse;
function getDir():Void {
    var new_x:Number = this._xmouse;
    new_x > old_x ? dir="right" : dir="left";
    old_x = new_x;
}
this.onMouseMove = getDir;

```

And here is the code I have used on one of my movieclips:

```auto
onClipEvent(enterFrame){
 this._x = _root._xmouse;
 this._y = _root._ymouse;
 if(_root.dir = "left"){
  this._xscale = -100; }
 if(_root.dir = "right"){
  this._xscale = 100; }
  trace(_root.dir);
 }

```

All I get is “right” being displayed?

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [April 15, 2006, 11:38am UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584/5 "2006-04-15T11:38:59Z")

</div>

because youre only tracing the right

see:  
[http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics\_of\_actionscript\_for\_flash&p=1&l=Flash\_MX\_04](http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=1&l=Flash_MX_04)

though I dont doubt you know that already, just in case:)

---

<div class="post-metadata">

**Author:** ![abstroose](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@abstroose](https://forum.kirupa.com/u/abstroose)\
**Post date:** [April 15, 2006, 11:48am UTC](https://forum.kirupa.com/t/detecting-mouse-direction/185584/6 "2006-04-15T11:48:10Z")

</div>

I thought the problem was that I am seeting the variable inside of a function.

```auto
function getDir():Void {
    var new_x:Number = this._xmouse;
    new_x > old_x ? dir = "right" : dir="left";
    old_x = new_x;
}

```

When I enter _trace(dir);_ outside of the function it returns undefined. However inside of the function and it operates correctly.

I haven’t used flash for about a year, but from what I remember I thought I needed to set it as a global var but still it returns undefined outside of the function.
