Event.MOUSE_LEAVE Implementation

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.


Posted

in

,

by

Tags:

Comments

8 responses to “Event.MOUSE_LEAVE Implementation”

  1. Ryan White Avatar
    Ryan White

    I find this does not work in Firefox 3.5 on a PC, the mouse_leave event never fires if you click and drag off the movie and then release.

    1. jimisaacs Avatar

      This is a problem with your version of firefox and the flash plugin support for that browser, or vice versa. What I have described is the correct way this event is fired normally. If there is a discrepancy, it is browser related, and I have to say most problems with mouse events and flash have always had to do with firefox.

      With this case I would suggest you run some tests. I for one, am not for browser checks and believe a developer should come to a common cross browser solution. (Unless you are talking about IE6 and CSS 😉
      So.. what I would do is first test regular mouse events such as MOUSE_UP and MOUSE_DOWN and see how they behave in this browser. Then you should try to find a merging point between the use of these events and their listeners with this MOUSE_LEAVE event implementation.

      What I am predicting, and I could be wrong, is that when you hold the mouse down and drag out from the stage, then release, one of the regular mouse events will fire such as MOUSE_UP, MOUSE_OUT, ROLL_OUT, etc…

      What you have to do is find out exactly what Firefox is doing with that mouse release outside of the stage. If after all your testing you find that there is absolutely nothing Firefox is doing, then for this version of this browser you and everyone else is out of luck.

      It is then a problem Adobe and Mozilla should address.
      (Essentially it already is since the Flash Player's number one selling point is browser compatibility, and dispatching events differently from browser to browser is definitely not a keen example of this.)

  2. Rob Avatar
    Rob

    Neither does it work on IE8 with FP10, or should I say most of the time it doesn't.

    1. jimisaacs Avatar

      This sounds like the MOUSE_UP bug that used to happen in all browsers for an application I wrote back in 2007. I was using MOUSE_OUT for a kind of stage roll out event, but I noticed it behaved differently at different points in time, like it was being fired differently and randomly. This went away with IE7, FF3, Safari 3, and even Chrome. But it seems things are reverting back to the way they were. That's all I can really say. I would do the same tests as what I said above your comment, but if you are getting random results, it could be any number of problems. Although I really do doubt it is random, would love to here more about what's going on if you test some more.

    2. jimisaacs Avatar

      This sounds like the MOUSE_UP bug that used to happen in all browsers for an application I wrote back in 2007. I was using MOUSE_OUT for a kind of stage roll out event, but I noticed it behaved differently at different points in time, like it was being fired differently and randomly. This went away with IE7, FF3, Safari 3, and even Chrome. But it seems things are reverting back to the way they were. That's all I can really say. I would do the same tests as what I said above your comment, but if you are getting random results, it could be any number of problems. Although I really do doubt it is random, would love to here more about what's going on if you test some more.

  3. Graeme Avatar
    Graeme

    MOUSE_LEAVE doesnt work at all when dragging if the wmode is set. It is an issue that has been flagged. This may be solvable with javascript, i.e. when the div the flash is contained within is rolled off, fire the function with external interface.

    1. John Avatar
      John

      I've been able to get Mouse_Leave event to work even with wmode param set in Firefox.

      However dispatching a Mouse_Up event in that situation has limited results: Flash recognizes the dispatched Mouse_Up event as having been fired, but does not treat it the same a genuine Mouse_Up event.. the drag (or in my case, the text highlighting anchor) remains in the Mouse_Down state. Really, really lame.

      Although this is a new problem for me, i've found references similar to Graeme's post dating back a long time… Should I be disappointed with Firefox or Adobe in not fixing this in over 3 years?

  4. Thanks! it’s helpful to me. Could you please read the cool Amcor split air conditioner info!…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.