Pages

Google+

Monday, June 7, 2010

AS2 Detecting When Mouse Leaves the Stage

A handy script when working with Actionscript 2 code. Because there is no Actionscript 3 Event.MOUSE_LEAVE equivalent in AS2. the best thing to do is to write a custom script for mouse detection on stage. I don't know who authored this, but I'd like to record it so it doesn't get lost on the internet.

     var mouse_dx:Number=_xmouse;
var mouse_dy:Number=_ymouse;
var mouseSpeed:Number=1;

function checkPosition(Void):Void
{
if(_xmouse(Stage.width-mouseSpeed) || _ymouse(Stage.height-mouseSpeed))
{
in_mc._visible=false;
out_mc._visible=true;
trace("out");
}
else
{
in_mc._visible=true;
out_mc._visible=false;
trace("in");
}
}

checkPosition(Void);

var mouseListener:Object = new Object();
mouseListener.onMouseMove = function():Void
{
mouse_dx = Math.abs(mouse_dx-_xmouse);
mouse_dy = Math.abs(mouse_dy-_ymouse);
mouseSpeed = mouse_dx > mouse_dy ? mouse_dx : mouse_dy;
mouseSpeed +=1;
checkPosition(Void);
mouse_dx=_xmouse;
mouse_dy=_ymouse;
}

Mouse.addListener(mouseListener);