View comments | RSS feed

EventDispatcher.addEventListener()

Availability

Flash Player 6 (6.0 79.0).

Edition

Flash MX 2004 and Flash MX Professional 2004.

Usage

componentInstance.addEventListener(event, listener)

Parameters

event  A string that is the name of the event.

listener A reference to a listener object or function.

Returns

Nothing.

Description

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.

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.

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.


Comments


jepo said on Jun 6, 2005 at 4:21 PM :
Thank you for your comments. This site is for documentation feedback only. Some comments with general questions about how to use components, bug reports, or feature requests for the Flash product, have been removed. Please use the Flash webforums for questions about how to use components: http://webforums.macromedia.com/flash. Please use this form for feature requests or suspected bugs: www.macromedia.com/support/email/wishform/.
dessita said on Jun 10, 2005 at 8:43 AM :
there is something I couldn't solve after searching long time in the Help about AddEventListener command. How to addEventListener to 1 movieClip or another object which is not component Instance? Is there a way at all? or some way to convert a button created by me into mx.component.button?..
huanshan said on Jul 10, 2005 at 1:10 PM :
I have a eventListener defined inside a constructor.
Somehow, I cannot access class variables inside the eventListener.
See code segment below:

class X {
private var v:Number = 1;

function X (mc:MovieClip) {
mc.createClassObject(mx.controls.Button, "aButton", ...);
var buttonListener:Object = new Object();
buttonListener.click = function(evt_obj:Object) {
trace(v); // displays undefined
}
mc.aButton.addEventListener("click", buttonListener);
}
}
No screen name said on May 18, 2006 at 12:10 AM :
class X {
private var v:Number = 1;

function X (mc:MovieClip) {
mc.createClassObject(mx.controls.Button, "aButton", ...);
var buttonListener:Object = new Object();
buttonListener.canvas = this;
buttonListener.click = function(evt_obj:Object) {

trace(this.canvas.v); // displays undefined

}
mc.aButton.addEventListener("click", buttonListener);
}
}

or

use "function handleEvent(evt){"

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00002444.html