User Guide
58 Chapter 4: Handling Component Events
// your code here
};
Tip: The
evtObj parameter is an object that is automatically generated when an event is triggered and
passed to the callback function. The event object has properties that contain information about the
event. For details, see “About the event object” on page 66.
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() on page 416.
For information about the events a component broadcasts, see the component’s entry in
Chapter 6, “Components Dictionary,” on page 91. For example, Button component events
are listed in the Button component section (or Help > Using Components > Components
Dictionary > 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 > Development Panels > Actions.