User Guide

502 EventDispatcher class
You can register multiple listeners to a single component instance, but you must use a separate
call to
addEventListener() for each listener. Also, you can register one listener to multiple
component instances, but you must use a separate call to
addEventListener() for each
instance. For example, the following code defines one listener object and assigns it to two
Button component instances, whose
label properties are button1 and button2, respectively:
lo = new Object();
lo.click = function(evt){
trace(evt.target.label + " clicked");
}
button1.addEventListener("click", lo);
button2.addEventListener("click", lo);
Execution order is not guaranteed. You cannot expect one listener to be called before another.
An event object is passed to the listener as a parameter. The event object has properties that
contain information about the event that occurred. You can use the event object inside the
listener callback function to access information about the type of event that occurred and
which instance broadcast the event. In the example above, the event object is
evt (you can use
any identifier as the event object name), and it is used in the
if statements to determine
which button instance was clicked. For more information, see “About the event object” in
Using Components.
Example
The following example defines a listener object, myListener, and defines the callback
function for the
click event. It then calls addEventListener() to register the myListener
listener object with the component instance
myButton.
myListener = new Object();
myListener.click = function(evt){
trace(evt.type + " triggered");
}
myButton.addEventListener("click", myListener);
To test this code, place a Button component on the Stage with the instance name myButton,
and place this code in Frame 1.