User Guide
Using listeners to handle events 61
4.
Select Frame 1 in the Timeline.
5.
Select Window > Development Panels > Actions.
6.
In the Actions panel, enter the following code:
// declare variables
var myList:mx.controls.List;
var myHandler:Function;
// add items to the list
myList.addItem("Bird");
myList.addItem("Dog");
myList.addItem("Fish");
myList.addItem("Cat");
myList.addItem("Ape");
myList.addItem("Monkey");
// define myHandler function
function myHandler(eventObj:Object){
// use the eventObj parameter
// to capture the event type
if (eventObj.type == "change"){
trace("The list changed");
} else if (eventObj.type == "scroll"){
trace("The list was scrolled");
}
}
// Register the myHandler function with myList.
// When an item is selected (triggers the change event) or the
// list is scrolled, myHandler executes.
myList.addEventListener("change", myHandler);
myList.addEventListener("scroll", myHandler);
Note: The
type property of the event object, evt, is a reference to the event name.
7.
Select Control > Test Movie; then select an item in the list and scroll the list to see the results
in the Output panel.
Caution: In a listener function, the keyword this refers to the component instance that calls
addEventListener(), not to the Timeline or the class where the function is defined. However, you can
use the Delegate class to delegate the listener function to a different scope. See “Delegating events”
on page 63. To see an example of function scoping, see the next section.
About scope in listeners
Scope refers to the object within which a function executes. Any variable references within that
function are looked up as properties of that object. You can use the Delegate class to specify the
scope of a listener. For more information, see “Delegating events” on page 63.