Packageflash.events
Classpublic class EventDispatcher
InheritanceEventDispatcher Inheritance Object
ImplementsIEventDispatcher
SubclassesCamera, ContextMenu, ContextMenuItem, DisplayObject, FileReference, FileReferenceList, IME, LoaderInfo, LocalConnection, Microphone, NetConnection, NetStream, PrintJob, SharedObject, Socket, Sound, SoundChannel, StyleSheet, Timer, URLLoader, URLStream, XMLSocket

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 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 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, while 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 class to gain event dispatching capabilities is to extend EventDispatcher. In cases where this is impossible, where the class is already extending another class, the author can instead implement the IEventDispatcher interface, create an EventDispatcher member, and write simple hooks to route calls into the aggregated EventDispatcher.

View the examples.

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods
 FunctionDefined by
  
The EventDispatcher class is generally used as a base class, which means that most users do not need to use this constructor function.
EventDispatcher
  
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.
EventDispatcher
  
Dispatches an event into the event flow.
EventDispatcher
  
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
  
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
  
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Constructor detail
EventDispatcher constructor

public function EventDispatcher(target:IEventDispatcher = null)

The EventDispatcher class is generally used as a base class, which means that most users do not need to use this constructor function. Advanced programmers, however, who are implementing the IEventDispatcher interface need to use this constructor. If you are unable to extend the EventDispatcher class and must instead implement the IEventDispatcher interface, this constructor can be used to aggregate an instance of the EventDispatcher class.

Parameters
target:IEventDispatcher (default = null) — Specifies the target object for events dispatched to the EventDispatcher object. This parameter is used when the EventDispatcher is aggregated by a class that implements IEventDispatcher—it is necessary so that the containing object can be the target for events. In simple cases where a class extends EventDispatcher, the target parameter should not be used.
Method detail
addEventListener method

public function 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. Event listeners can be registered on all nodes in the display list tree for a specific type of event, phase, and priority.

Once an event listener is successfully registered:

It is not possible to register an event listener for only one of the target and bubbling phases since those phases are coupled during registration. They are coupled because bubbling applies only to the ancestors of the target node.

You should remove an event listener when it is no longer needed by calling EventDispatcher.removeEventListener(). Failure to remove unnecessary event listeners may have a negative impact on memory usage. Any objects with registered event listeners are not removed from memory because the garbage collector does not remove objects that still have references.

Copying an EventDispatcher does not copy the event listeners attached to it. Event listeners must be attached to the newly created node afterwards if so desired.

Moving an EventDispatcher does not affect the event listeners attached to it.

If the event listener is being registered on a node while an event gets processed on this node, the event listener is not triggered during the current phase but may be triggered during a later phase in the event flow, such as the bubbling phase.

If an event listener is removed from a node while an event is being processed on the node, it is still triggered by the current actions. Once removed, the event listener is never invoked again (unless registered again for future processing).

For the Stage class, which extends the EventDispatcher class, the addEventListener() method is only available to an object that is in the same security sandbox as the Stage owner (the first SWF file loaded). The Stage owner can grant permission to the domain of the calling object by calling the Security.allowDomain() or Security.allowInsecureDomain() method. For more information, see the "Security" chapter in Programming ActionScript 3.0.

Parameters
type:String — Specifies the type of event.
listener:Function — The listener object that will process the event. This function must accept an Event object as its only parameter and must return nothing, for example:

function(evt:Event):void

The function may have any name.
useCapture:Boolean (default = false) — Determines whether the listener works in the capture phase or the target and bubbling phases. If useCapture is set to true, the event listener processes the event only during the capture phase and not in the target or bubbling phases. If false, then the event listener will only process the event during the target or bubbling phases. To listen for the event in all three phases, call addEventListener twice, once with useCapture set to true, then again with useCapture set to false.
priority:int (default = 0) — Determines the priority level of the event listener. Priorities are assigned using the int data type, which is a signed 32-bit integer. The higher the number, the higher the priority. All listeners with priority N are processed before listeners of priority N-1. If two or more listeners share the same priority, they are processed in the order in which they were added. The default value of this parameter is 0.
useWeakReference:Boolean (default = false) — Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage collected. A weak reference does not. The default value of this parameter is false.
dispatchEvent method

public function dispatchEvent(event:Event):Boolean

Dispatches an event into the event flow. The event target is the EventDispatcher object upon which dispatchEvent is called.

For the Stage class, which extends the EventDispatcher class, the dispatchEvent() method is only available to an object that is in the same security sandbox as the Stage owner (the first SWF file loaded). The Stage owner can grant permission to the domain of the calling object by calling the Security.allowDomain() or Security.allowInsecureDomain() method. For more information, see the "Security" chapter in Programming ActionScript 3.0.

For the LoaderInfo class, which extends the EventDispatcher class, calling the dispatchEvent() method throws an exception.

Parameters
event:Event — The Event object that is dispatched into the event flow. If the event is being redispatched, a clone of the event is created automatically. Once an event is dispatched the target property cannot be changed, so a new copy of the event must be created in order for redispatching to work.

Returns
Boolean — Returns true unless preventDefault() is called on the event, in which case false.
hasEventListener method

public function hasEventListener(type:String):Boolean

Checks whether the EventDispatcher object has any listeners registered for a specific type of event. This allows you to determine where altered handling of an event type has been introduced in the event flow hierarchy by an EventDispatcher object. To determine whether a specific event type actually triggers an event listener, use EventDispatcher.willTrigger().

The difference between hasEventListener and willTrigger is that hasEventListener examines only the object to which it belongs, whereas willTrigger examines the entire event flow for the event specified by the type parameter.

When the hasEventListener method is called from a LoaderInfo object only the listeners that the caller can access are considered.

For the Stage class, which extends the EventDispatcher class, the hasEventListener() method is only available to an object that is in the same security sandbox as the Stage owner (the first SWF file loaded). The Stage owner can grant permission to the domain of the calling object by calling the Security.allowDomain() or Security.allowInsecureDomain() method. For more information, see the "Security" chapter in Programming ActionScript 3.0.

Parameters
type:String — Specifies the type of event.

Returns
Boolean — Returns true if a listener of the specified type is registered, and returns false otherwise.

See also
willTrigger()
removeEventListener method

public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void

Removes a listener from the EventDispatcher object. If there is no matching listener registered with the EventDispatcher object, then calling this method has no effect.

Parameters
type:String — Specifies the type of event.
listener:Function — The listener object to remove.
useCapture:Boolean (default = false) — Specifies whether the listener was registered for the capture phase or the target and bubbling phases. If the listener was registered for both the capture phase and the target and bubbling phases, two calls to removeEventListener() are required to remove both, one call with useCapture() set to true, and another call with useCapture() set to false.
willTrigger method

public function 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. This method returns true if an event listener will be triggered during any phase of the event flow when an event of the specified type is dispatched to this EventDispatcher object or any of its descendants.

The difference between hasEventListener() and willTrigger() is that hasEventListener() examines only the object to which it belongs, whereas willTrigger() examines the entire event flow for the event specified by the type parameter.

When the willTrigger method is called from a LoaderInfo object only the listeners that the caller can access are considered.

For the Stage class, which extends the EventDispatcher class, the willTrigger() method is only available to an object that is in the same security sandbox as the Stage owner (the first SWF file loaded). The Stage owner can grant permission to the domain of the calling object by calling the Security.allowDomain() or Security.allowInsecureDomain() method. For more information, see the "Security" chapter in Programming ActionScript 3.0.

Parameters
type:String — Specifies the type of event.

Returns
Boolean — Returns true if a listener of the specified type will be triggered, and returns false otherwise.
Class examples

The following example uses the classes EventDispatcherExample and CustomDispatcher, a subclass of EventDispatcher, to show how a custom event is created and dispatched. This is accomplished using the following steps:
  1. The constructor of EventDispatcherExample creates a local variable dispatcher and assigns it to a new CustomDispatcher object instance.
  2. Inside CustomDispatcher, a string is set so that the event has the name action and the doAction() method the action event is created and dispatched using EventDispatcher.dispatchEvent() when the method is called.
  3. The dispatcher property is then used to add the action event listener and associated subscriber method actionHandler(), which simply prints out text about the event when it is dispatched using trace().
  4. The doAction() method is invoked, which dispatches 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));
    }
}




 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/labs/flashauthoringpreview/flash/events/EventDispatcher.html