AS2 → AS3: Using onReleaseOutside

Download Example Files

One of the things that is weird to figure out about AS3 is why a MOUSE_UP_OUTSIDE event (or something similar) was not included. This leaves for work-arounds that mimic that behavior and this is one way I've seen it done (probably not the only way, but one of the ways to skin a cat). This is the AS2 version:

Actionscript:
  1. box_mc.onPress = function():Void
  2. {
  3.     trace("PRESSED");
  4. };
  5.  
  6. box_mc.onRelease = function():Void
  7. {
  8.     trace("RELEASED");
  9. };
  10.  
  11. box_mc.onReleaseOutside = function():Void
  12. {
  13.     trace("RELEASED OUTSIDE");
  14. };

And here is the AS3 version, not as pretty, but effective:

Actionscript:
  1. box_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
  2. box_mc.buttonMode = true;
  3.  
  4. function handleMouseDown($evt:MouseEvent):void
  5. {
  6.     box_mc.alpha = .5;
  7.    
  8.     stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
  9. }
  10.  
  11. function handleMouseUp($evt:MouseEvent):void
  12. {
  13.     box_mc.alpha = 1;
  14.    
  15.     stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
  16. }

As you can see, its a bit of a hack, but it works quite well. So far I haven't had any problems using it in this manner.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

1 Comment

thank you - exact that what i was looking for

Leave a comment

(required)

(required)