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 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.

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.


Version 8

Comments


No screen name said on Jul 3, 2007 at 11:14 AM :
This is just not working for me. FYI, I DO have the button instance on stage named correctly. Is there something I'm missing?
Iglota said on May 27, 2008 at 10:58 AM :
I d'like to know what are all the possible EventDispatcher when using addEventListener(). I just know scroll and click.

Tks,

Will
monitor69 said on Jul 25, 2008 at 5:31 PM :
Not sure if anyone can help, I'm trying to do something really complicated.

I need to create a rollover. Tricky hey?

It sounds like one of the easiest things to do, but I cant find any examples anywhere.

I don't mean "button.onRollOver" - I need a rollover listener inside a custom class, and all of the examples show the following line...

myButton.addEventListener("click", myListener);

I need to use something like this...

myButton.addEventListener("rollOver", myListener);

What is the event for "rollover"? Please help - I'm losing my mind!

I've tried all the combinations of mouseover rollover onrollover - all with various uppercase and lowercase combinations and nothing works.

I've also tried MouseEvent.ROLL_OVER with quotes and without, and with import flash.events.MouseEvent or import flash.events.*
This causes a compile error.

Lots of other people are having problems with this, but there's some confusion between AS2 and AS3. I need AS2 and I'm amazed at how something so simple has not been properly documented.

Hope someone can help,

Cheers

 

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