![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
Flash Player 6.0.79.
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 is triggered, 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 is triggered. 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 is triggered. For more information, see Using component event listeners.
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:
lo = new Object(); lo.click = function(evt){ if (evt.target == button1){ trace("button 1 clicked"); } else if (evt.target == button2){ trace("button 2 clicked"); } } button1.addEventListener("click", lo); button2.addEventListener("click", lo);
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 within the if
statements to determine which button instance was clicked. For more information, see Event Objects.
The following example defines a listener object, myListener
, and defines the callback function click. It then calls addEventListener()
to register the myListener
listener object with the component instance myButton
. To test this code, place a button component on the Stage with the instance name myButton
, and place the following code in Frame 1:
myListener = new Object(); myListener.click = function(evt){ trace(evt.type + " triggered"); } myButton.addEventListener("click", myListener);
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |