Detecting Mouse Direction

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

I’m using the following script:


[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.

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

it works fine for me too:)

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:

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:

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?

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

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

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

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.