| Package | flash.events |
| Class | public class EventDispatcher |
| Inheritance | EventDispatcher Object |
| Implements | IEventDispatcher |
| Subclasses | Camera, ContextMenu, ContextMenuItem, DisplayObject, FileReference, FileReferenceList, IME, LoaderInfo, LocalConnection, Microphone, NetConnection, NetStream, PrintJob, SharedObject, Socket, Sound, SoundChannel, StyleSheet, Timer, URLLoader, URLStream, XMLSocket |
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.| Function | Defined by | ||
|---|---|---|---|
|
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.
| 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 | ||
![]() |
Indicates whether an object has a specified property defined.
| Object | |
![]() |
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter.
| Object | |
![]() |
Indicates whether the specified property exists and is enumerable.
| Object | |
|
Removes a listener from the EventDispatcher object.
| EventDispatcher | ||
![]() |
Sets the availability of a dynamic property for loop operations.
| Object | |
![]() |
Returns the string representation of the specified object.
| Object | |
![]() |
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 | ||
public function EventDispatcher(target:IEventDispatcher = null)
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 IEventDispatcherit 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.
|
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Once an event listener is successfully registered:
removeListener before registering the listener again with the new priority level;type
or useCapture result in the creation of a separate listener registration.
For example, if you first register a listener with useCapture set to
true, it listens only during the capture phase. If you subsequently call
this method again using the same listener object, but with useCapture
set to false, you have two separate listeners, one that listens during the
capture phase and another that listens during the target and bubbling phases.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.
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:
|
|
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.
|
public function dispatchEvent(event:Event):Boolean
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.
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.
|
Boolean —
Returns true unless preventDefault() is called on the event, in which case false.
|
public function hasEventListener(type:String):Boolean
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.
type:String — Specifies the type of event.
|
Boolean —
Returns true if a listener of the specified type is registered,
and returns false otherwise.
|
| willTrigger() |
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
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.
|
public function willTrigger(type:String):Boolean
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.
type:String — Specifies the type of event.
|
Boolean —
Returns true if a listener of the specified type will be triggered, and returns false otherwise.
|
EventDispatcherExample and
CustomDispatcher, a subclass of EventDispatcher, to show how a
custom event is created and dispatched. This is accomplished using the following steps:
EventDispatcherExample creates a local variable
dispatcher and assigns it to a new CustomDispatcher object instance.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.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().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