Past Weeks New Content
Search CMX

Advanced Search

Latest Free Content
View All
Free Content
Accessibility

The AS3 Event System - Part 1: The Basics

By: Steve Schelter

Page 4 of 4

Set for printing

Previous

STEP 3) Dispatch events

Now that the linkage is complete, all that's left is to dispatch "testEvent" events! In AS3.0, event objects act as messengers between dispatchers and listeners, and therefore an Event or Event subclass must be instantiated every time a dispatch is made, as these event objects contain all the necessary information for a successful dispatch. The bare minimum that you must contribute to event objects is the type of event, which corresponds to the event type mentioned in STEP 2.

Even though the DispatchEvent() method is always public, its traditionally used privately by dispatchers (or a single parent if the class is implementing IEventDisptacher instead of extending EventDispatcher). For the sake of our demonstration, I've exposed a method in the DispatcherObject instances to call their DispatchEvent().

Setup 3
Dispatchers can now send event objects to its listeners.

Add the following code to DispatcherObject.as and EventsDemo.as

//DispatcherObject.as

package{
    import flash.events.EventDispatcher;
    import flash.events.Event;

    public class DispatcherObject extends
    continue EventDispatcher{
        private var label:String;

        public function DispatcherObject(label:String){
            this.label = label;
        }

        public override function toString():String{
            return "[ Dispatcher "+label+" ]";
        }

        public function dispatch():void{
            this.dispatchEvent(new Event("testEvent"));
        }
    }
}

//EventsDemo.as

package{
    import flash.display.Sprite;

    public class EventsDemo extends Sprite{

        public function EventsDemo(){
            var dispatcher1:DispatcherObject =
            continue new DispatcherObject("D1");
            var dispatcher2:DispatcherObject =
            continue new DispatcherObject("D2");

            var listener1:ListenerObject =
            continue new ListenerObject("L1");
            listener1.addDispatcher(dispatcher1);

            var listener2:ListenerObject =
            continue new ListenerObject("L2");
            listener2.addDispatcher(dispatcher2);

            var listener3:ListenerObject =
            continue new ListenerObject("L3");
            listener3.addDispatcher(dispatcher1);
            listener3.addDispatcher(dispatcher2);

            dispatcher1.dispatch();
            dispatcher2.dispatch();
        }
    }
}

STEP 4) Testing our code

Run EventsDemo.as in debug mode to observe the traces below. As you can see, each dispatcher can have many listeners, and each listener can register to many dispatchers.

testEvent dispatched from [ Dispatcher D1 ] to [ Listener L1 ]
testEvent dispatched from [ Dispatcher D1 ] to [ Listener L3 ]
testEvent dispatched from [ Dispatcher D2 ] to [ Listener L2 ]
testEvent dispatched from [ Dispatcher D2 ] to [ Listener L3 ]

Page 4 of 4 Previous 1 2 3 4


download
Download Support Files


Keywords
event, events, listener, listeners, dispatcher, dispatchers, eventdispatcher, eventdispatchers, addEventDispatcher, dispatchEvent, AS3.0, actionscript 3.0, actionscript