Screenwriter, Choreographer, Game Developer

Events in Actionscript 3

Posted on

There was lots of positive feedback to yesterday's post about my Actionscript 2 event class, so I decided to create an Actionscript 3 version. I added a lot of features while I was at it - listener priorities, preventDefault, stopNotification (aka stopImmediatePropagation), hasListeners, weak references, and to send the event as the single parameter when notifying listeners.

Basically it implements almost all the features in the Actionscript 3 event model, but with all the functionallity contained within the event class itself. This way events are identified as properties of the object rather than by Strings (or by static properties containing those strings).

A simple example of its use would be this clicked event

package
{
   import bigroom.BrEvent;
   import flash.display.*;
   import flash.events.MouseEvent;
   
   class Listenable extends Sprite
   {
      public var clicked:BrEvent;
      
      public function Listenable() {
         clicked = new BrEvent( this );
         graphics.beginFill( 0xFF0000 );
         graphics.drawCircle( 50, 50, 50 );
         addEventListener( MouseEvent.MOUSE_DOWN, mouseListener );
      }
      
      public function mouseListener( ev:MouseEvent ):void {
         clicked.notify();
      }
   }
}
package
{
   import flash.display.*;
   
   public class Test extends Sprite
   {
      public function Test() {
         var listen:Listenable = new Listenable();
         addChild( listen );
         listen.clicked.addListener( doClick );
      }
      
      public function doClick( ev:BrEvent ):void {
         trace( "clicked" );
      }
   }
}

Download source code Download the BrEvent classes.

I first tried to implement this using entirely custom code, and everything worked fine except the weak references. I used the weak references as described in this post, and came across the problem described here. This implementation is BrEvent1 in the download. It's great if you don't use the weak references, and interesting anyway.

So then I implemented it utilising the existing Event and EventDispatcher classes and that works fine. This implementation is BrEvent2 in the download. There's a couple of redundant methods calling methods in the super class but they're there to make the point about the overriden methods.

The one feature of the Actionscript 3 event model this class doesn't implement is the capture and bubbling phases of events on display objects. The reason is, it's not clear what events should be called in the various display objects up the chain. Unless I somehow hack the chosen events into the DisplayObject class itself, the events will only exists on selected display objects. So I didn't implement this.

Having done this, I still don't expect to use it. Although I prefer it to the built-in event handling in Actionscript 3, the built in event handling is good enough so I will use that for consistency.

So it was really just a short learning experience. It didn't take long and I got to play meaninglessly with Actionscript for a morning. If you have any suggestions or you find the class(es) useful, let me know below.

Also, if you can think of a better name, let me know that too. I wanted to simply call the class Event but that would be too confusing with the built-in Event class.

Share this post or a comment online -


Also in the collection Actionscript