User Guide

66 Handling Component Events
Finally, you call the addEventListener() method from the component instance that
broadcasts the event. The
addEventListener() method takes two parameters: a string
indicating the name of the event and a reference to the listener object.
componentInstance.addEventListener("eventName", listenerObject);
Here is the whole code segment, which you can copy and paste. Be sure to replace any code in
italics with actual values; you can use
listenerObject and evtObj or any other legal
identifiers, but you must change
eventName to the name of the event.
var listenerObject:Object = new Object();
listenerObject.eventName = function(evtObj:Object){
// code placed here executes
// when the event is triggered
};
componentInstance.addEventListener("eventName", listenerObject);
The following code segment uses the this keyword as the listener object:
function eventName(evtObj:Object){
// code placed here executes
// when the event is triggered
}
componentInstance.addEventListener("eventName", this);
You can call addEventListener() from any component instance; it is mixed in to every
component from the EventDispatcher class. (A mix-in” is a class that provides specific
features that augment the behavior of another class.) For more information, see
“EventDispatcher.addEventListener()” in the Components Language Reference.
For information about the events a component broadcasts, see the component’s entry in the
Components Language Reference. For example, Button component events are listed in the
Button component section (or Help > Components Language Reference > Button
component > Button class > Event summary for the Button class).
To register a listener object in a Flash (FLA) file:
1. In Flash, select File > New and create a new Flash document.
2. Drag a Button component to the Stage from the Components panel.
3. In the Property inspector, enter the instance name myButton.
4. Drag a TextInput component to the Stage from the Components panel.
5. In the Property inspector, enter the instance name myText.
6. Select Frame 1 in the Timeline.
7. Select Window > Actions.