Saturday 12 October 2013

History of EventDispatcher in AS3



The EventDispatcher class is the base class for all classes that dispatch events. The EventDispatcher class implements the IEventDispatcher interface and is the base class for the DisplayObject class. The EventDispatcher class allows any object on the display list to be an event target and as such, to use the methods of the IEventDispatcher interface.
Event targets are an important part of the Flash Player and Adobe AIR event model. The event target serves as the focal point for how events flow through the display list hierarchy. When an event such as a mouse click or a keypress occurs, Flash Player or the AIR application dispatches an event object into the event flow from the root of the display list. The event object then makes its way through the display list until it reaches the event target, at which point it begins its return trip through the display list. 

This round-trip journey to the event target is conceptually divided into three phases: 
the capture phase comprises the journey from the root to the last node before the event target's node, 
the target phase comprises only the event target node, and 
the bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the display list.

In general, the easiest way for a user-defined class to gain event dispatching capabilities is to extend EventDispatcher. If this is impossible (that is, if the class is already extending another class), you can instead implement the IEventDispatcher interface, create an EventDispatcher member, and write simple hooks to route calls into the aggregated EventDispatcher.

Public Methods :-

EventDispatcher(target:IEventDispatcher = null)
Aggregates an instance of the EventDispatcher class.

addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.

dispatchEvent(event:Event):Boolean
Dispatches an event into the event flow.

hasEventListener(type:String):Boolean
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.

removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.

willTrigger(type:String):Boolean
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.


Events :- 

Activate :- Dispatched when the Flash Player or AIR application gains operating system focus and becomes active
Deactivate :- Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.


Sample Code :- 


The following example uses the classes EventDispatcherExample and CustomDispatcher, a subclass of EventDispatcher, to show how a custom event is created and dispatched. The example carries out the following tasks:
The constructor of EventDispatcherExample creates a local variable dispatcher and assigns it to a new CustomDispatcher instance.
Inside CustomDispatcher, a string is set so that the event has the name action, and the doAction() method is declared. When called, this method creates the action event and dispatches it using EventDispatcher.dispatchEvent().
The dispatcher property is then used to add the action event listener and associated subscriber method actionHandler(), which simply prints information about the event when it is dispatched.
The doAction() method is invoked, dispatching the action event.
package {
    import flash.display.Sprite;
    import flash.events.Event;

    public class EventDispatcherExample extends Sprite {

        public function EventDispatcherExample() {
            var dispatcher:CustomDispatcher = new CustomDispatcher();
            dispatcher.addEventListener(CustomDispatcher.ACTION, actionHandler);
            dispatcher.doAction();
        }

        private function actionHandler(event:Event):void {
            trace("actionHandler: " + event);
        }
    }
}




import flash.events.EventDispatcher;
import flash.events.Event;

class CustomDispatcher extends EventDispatcher {
    public static var ACTION:String = "action";

    public function doAction():void {
        dispatchEvent(new Event(CustomDispatcher.ACTION));
    }
}

Download Source code

No comments:

Post a Comment