The Event.MOUSE_LEAVE
event is a little confusing as it is not a MouseEvent
.
But something important about it, is that it is the way to create a “mouseUp” event when the mouse is outside of the stage.
It is done with controlling a simple Boolean
variable you may set called “dragging” or “mouseDown”, something of that sort. There are a lot of ways to handle it with the hasEventListener
and/or willTrigger
methods but I will stay within the much simpler variable implementation for this post.
When the mouse button is up, and the cursor leaves the stage the MOUSE_LEAVE
event is fired. When the mouse button is pressed while over the stage, then the mouse leaves the stage while still pressed down, the MOUSE_LEAVE
event is not fired until the mouse button is released while outside of the stage. This is very useful because with a simple variable controlled within the MOUSE_LEAVE
event handler, you can create a kind of “stage release outside event handler”.
Because of the behavior of the event I just described, something like this will work:
private function handleStageMouseLeave(e:Event):void {
if(dragging) {
dragging = false;
// do stage mouse up events if any
} else {
// do stage mouse out events if any
}
}
Needless to say a nice way to use this event is for sliders, scrubbers, scrollers, or custom components in general. It is a best practice in my opinion to always handle the MOUSE_LEAVE
event whenever I bypass the built-in drag methods and deal with global stage events in order to create my own dragging functionality.
So in conclusion, this event isn’t much on it’s own. It must be combined with the use of other global stage events and custom variables to use it for its real, or in my opinion, correct purpose.
Leave a Reply