[AS3 SNIPPET] Detect if mouse pointer is over/out of the stage area

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
stage.addEventListener (Event.MOUSE_LEAVE, leaveHandler);
stage.addEventListener (MouseEvent.MOUSE_MOVE, returnHandler);

function leaveHandler(event:Event):void
{
    trace("leaveHandler");

    stage.addEventListener (MouseEvent.MOUSE_MOVE, returnHandler);
}

function returnHandler(event:Event):void
{
    trace("returnHandler");

    stage.removeEventListener(MouseEvent.MOUSE_MOVE, returnHandler);
}

Event.MOUSE_LEAVE: dispatched by the Stage object when the mouse pointer moves out of the stage area.
MouseEvent.MOUSE_MOVE: Dispatched when a user moves the pointing device while it is over an InteractiveObject.