User Guide
362 Handling Events
{
public interface IEventDispatcher
{
function addEventListener(eventName:String,
listener:Object,
useCapture:Boolean=false,
priority:Integer=0,
useWeakReference:Boolean=false):Boolean;
function removeEventListener(eventName:String,
listener:Object,
useCapture:Boolean=false):Boolean;
function dispatchEvent(eventObject:Event):Boolean;
function hasEventListener(eventName:String):Boolean;
function willTrigger(eventName:String):Boolean;
}
}
The Flash Player API implements the IEventDispatcher interface with the EventDispatcher
class, which serves as a base class for all classes that can be event targets or part of an event
flow. For example, the DisplayObject class inherits from the EventDispatcher class. This
means that any object on the display list has access to the methods of the IEventDispatcher
interface.
Adding event listeners
The addEventListener() method is the workhorse of the IEventDispatcher interface. You
use it to register your listener functions. The two required parameters are
type and listener.
You use the
type parameter to specify the type of event. You use the listener parameter to
specify the listener function that will execute when the event occurs. The
listener parameter
can be a reference to either a function or a class method.
The
useCapture parameter of the addEventListener() method allows you to control the
event flow phase on which your listener will be active. If
useCapture is set to true, your
listener will be active during the capture phase of the event flow. If
useCapture is set to
false, your listener will be active during the target and bubbling phases of the event flow. To
listen for an event during all phases of the event flow, you must call
addEventListener()
twice, once with
useCapture set to true, and then again with useCapture set to false.
NOTE
Do not use parentheses when you specify the listener parameter. For example, the
clickHandler() function is specified without parentheses in the following call to the
addEventListener() method:
addEventListener(MouseEvent.CLICK, clickHandler).