The easiest onReleaseOutside implementation in AS3?

I am a long-time user of ActionScript 2, now I'm starting with ActionScript 3. The only thing I miss is an easy way to duplicate the functionality of AS2 MovieClip.onReleaseOutside. It is almost always necessary to implement this event, otherwise you will get funny errors, as the flash thinks that your mouse does not work when it really is.

According to the AS2 to AS3 Migration Guide , I have to use flash.display.InteractiveObject.setCapture() for this, however it does not exist as far as I can tell. I assume this document is outdated or incorrect. I found several posts on the Internet on how to duplicate this functionality, but they either have their own problems:

  • This one triggers onReleaseOutside, even if there was no corresponding onPress event.
  • This option seems very inefficient, you will add and remove an event listener every time the mouse is clicked anywhere in your application.

There should be an easier way, don’t tell me that Adobe forgot about this when rewriting ActionScript?

Sample AS2 code:

 // Assume myMC is a simple square or something on the stage myMC.onPress = function() { this._rotation = 45; } myMC.onRelease = myMC.onReleaseOutside = function() { this._rotation = 0; } 

Without the onReleaseOutside handler, if you clicked on squre, dragged the mouse out of it and released the mouse, then the square will not rotate and seems to be stuck.

+4
source share
3 answers

Simple and reliable:

 button.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler ); button.addEventListener( MouseEvent.MOUSE_UP, buttonMouseUpHandler ); // * function mouseDownHandler( event : MouseEvent ) : void { trace( "onPress" ); // this will catch the event anywhere event.target.stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler ); } function buttonMouseUpHandler( event : MouseEvent ) : void { trace( "onRelease" ); // don't bubble up, which would trigger the mouse up on the stage event.stopImmediatePropagation( ); } function mouseUpHandler( event : MouseEvent ) : void { trace( "onReleaseOutside" ); event.target.removeEventListener( MouseEvent.MOUSE_UP, mouseUpHandler ); } 

If you do not care about the difference between onRelease and onReleaseOutside (for example, with draggable items), you will skip the mouse at the top of the listener on the button itself (commented here with an asterisk).

+10
source

You have watched this event:

 flash.events.Event.MOUSE_LEAVE 




From the documentation:

Dispatched by a Stage when the mouse moves out of the scene area. The Event.MOUSE_LEAVE constant defines the value of the type property of a mouseLeave event object.


It will solve your problem if you are only interested in a user mouse, if it is outside the scene, and not just outside this particular MovieClip.

+3
source

root.addEventListener (MouseEvent.UP, onMouseReleaseOutside);

Of course you define onMouseReleaseOutside. Basically, any MouseEvent.UP (mouse release) that happens outside of your button (or mc) gets into the scene instead of your button. This is how I usually catch him.

+3
source

All Articles