Flash 8 Documentation |
|||
| Components Language Reference > EventDispatcher class > EventDispatcher.addEventListener() | |||
Flash Player 6 (6.0.79.0).
Flash MX 2004 and Flash MX Professional 2004.
componentInstance.addEventListener(event,listener)
event A string that is the name of the event.
listener A reference to a listener object or function.
Nothing.
Method; registers a listener object with a component instance that is broadcasting an event. When the event occurs, the listener object or function is notified. You can call this method from any component instance. For example, the following code registers a listener to the component instance myButton:
myButton.addEventListener("click", myListener);
You must define the listener as either an object or a function before you call addEventListener() to register the listener with the component instance. If the listener is an object, it must have a callback function defined that is invoked when the event occurs. Usually, that callback function has the same name as the event with which the listener is registered. If the listener is a function, the function is invoked when the event occurs. For more information, see Using listeners to handle events in Using Components.
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.
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.
Version 8
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/8/main/00003474.html
Comments
No screen name said on Jul 3, 2007 at 11:14 AM : Iglota said on May 27, 2008 at 10:58 AM : monitor69 said on Jul 25, 2008 at 5:31 PM :