Flashr_logo

How To : Create a custom AS3 event with a body

Step 1

Flash and Flex have built in events such as MouseEvent.CLICK, but in some cases you might need to create a custom event for your application. One benefit of creating a custom event is that you can add an event body which is a payload that can goes along for the ride. Below is an example of a custom event with a body that can be any kind of object. If you know what kind of object your payload is going to be (String, XML, Array, etc) it's better to strongly type it to avoid errors.

package us.flashr.events
{
    import flash.events.Event;

    public class PayloadEvent extends Event
    {
        ///////////////////////////////////////////////////////////////////////
        //
        // CONSTANTS
        //
        ///////////////////////////////////////////////////////////////////////
       
        public static const COMPLETE:String = 'complete';
       
        ///////////////////////////////////////////////////////////////////////
        //
        // PRIVATE VARIABLES
        //
        ///////////////////////////////////////////////////////////////////////
       
        private var _body:Object;
       
        ///////////////////////////////////////////////////////////////////////
        //
        // GETTERS/SETTERS
        //
        ///////////////////////////////////////////////////////////////////////
       
        public function get body():Object { return _body; }
       
        ///////////////////////////////////////////////////////////////////////
        //
        // CONSTRUCTOR
        //
        ///////////////////////////////////////////////////////////////////////
       
        public function PayloadEvent(type:String, body:Object, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            _body = body;
            super(type, bubbles, cancelable);
        }
       
        ///////////////////////////////////////////////////////////////////////
        //
        // PUBLIC METHODS
        //
        ///////////////////////////////////////////////////////////////////////
       
        override public function clone():Event
        {
            return new PayloadEvent( type, body, bubbles, cancelable );
        }
    }
}

Step 2

To use your custom event dispatch it from any class that implements IEventDispatcher. In the example below the same class is dispatching and listening for the event.

package {
    import flash.display.Sprite;
   
    import us.flashr.events.PayloadEvent;

    public class CustomEvent extends Sprite
    {
        public function CustomEvent()
        {
            var object:Object = {name:"foo", value:"something"};
            this.addEventListener( PayloadEvent.COMPLETE, completeHandler );
            this.dispatchEvent( new PayloadEvent( PayloadEvent.COMPLETE, object ) );
        }
       
        private function completeHandler( e:PayloadEvent ):void
        {
            trace( e.body.name );
            trace( e.body.value );
        }
    }
}