my new favorite flash event handles the mouse out of stage problem

I don’t want to breed ill will by playing Actionscript favorites, but at the moment I definitely like Event.MOUSE_LEAVE a little more than the rest of the flash.events.Event types.

I’m working on a piece of functionality in my Flash as3 application that involves replacing the mouse cursor with a custom clip and making another clip change it’s position in response to MouseEvent.MOUSE_MOVE. The standard cursor is supposed to return and everything stops dragging on MouseEvent.MOUSE_UP. Standard stuff that any Flash developer has worked with before. What left me unsatisfied was detecting MOUSE_UP when the user has dragged the cursor off of the swf entirely. In this case the dragging action is still active until the user has brought the mouse back to the stage and clicked a couple times. There are all kinds of schemes from as2 for detecting position and watching for when the user leaves the stage, but they tend to break with rapid movements. Generally a hackfest of complex answers.

Enter my favorite new Event type:

stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);

It took me awhile of hunting around in the API Livedocs to find this gem, in part because I was focused on using MouseEvent for my solution and wasn’t looking at the main Event class. Interestingly enough, it appears in my case that the event is not dispatched until the user lets go of the mouse, so it’s more of a MOUSE_UP while offstage, but that’s the end result I wanted so now I’m satisfied.

Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

5 comments ↓

#1 Matt on 09.08.07 at 11:29 am

It didn’t do the trick for me. here is my code:

stage.addEventListener(Event.MOUSE_LEAVE, reset);
function reset(event:Event):void{trace(“mouseout”);};

seems simple enough, but when my mouse leaves the stage, no tracing. This has been an issue for me for weeks.

#2 jon on 09.08.07 at 11:41 am

In my project it appeared as though MOUSE_LEAVE is actually a listener for MOUSE_UP when the mouse is off the stage. It’s almost as if the MOUSE_LEAVE event is requires some other mouse event in order to actually fire. This behavior worked with my particular use case, but it certainly appears to be acting differently that advertised.

I would suggest using the following code to see what’s happening:

stage.addEventListener(Event.MOUSE_LEAVE, reset);
stage.addEventListener(Event.MOUSE_UP, reset);
stage.addEventListener(Event.MOUSE_DOWN, reset);
function reset(event:Event):void{trace(“reset: ” + event.type);};

#3 brook on 11.06.07 at 7:42 pm

hey matt — i don’t know if this is your issue but i just spent a day trying to figure out why a simple MOUSE_LEAVE event didn’t work.

turns out it dosen’t seem to work when i do a publish preview to a .swf file, but it works perfectly when i publish preview to a .html.

i guess the .swf has to be in the html??? sad and #%$&$!! but true.

#4 Richard Olsson on 02.11.08 at 2:01 pm

I’m having an even stranger problem, as my MOUSE_LEAVE stops triggering as soon as I turn on bitmap caching for a certain MovieClip. So if anyone else out there is having this problem, try turning cacheAsBitmap off if you don’t absolutely need it. I do though, so I guess I’m out of luck.

#5 Bookmarks about Event on 09.20.08 at 3:30 am

[...] – bookmarked by 5 members originally found by racamirko on 2008-08-25 my new favorite flash event handles the mouse out of stage problem http://marstonstudio.com/2007/08/23/my-new-favorite-flasheventsevent-type/ – bookmarked by 6 [...]

Leave a Comment