Flash CS3 Documentation |
|||
| Using ActionScript 2.0 Components > Handling Component Events > Using listeners to handle events > Using listener functions | |||
Unlike the handleEvent syntax, several listener functions can handle different events. So instead of having the if and else if checks in myHandler, you can just define myChangeHandler for the change event and myScrollHandler for the scroll event and register them, as shown here:
myList.addEventListener("change", myChangeHandler);
myList.addEventListener("scroll", myScrollHandler);
To use a listener function, you must first define a function:
functionmyFunction:Function(evtObj:Object){// your code here}
|
TIP |
|
The |
Then 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 function.
componentInstance.addEventListener("eventName",myFunction);
You can call addEventListener() from any component instance; it is included in every UI component from the EventDispatcher class. For more information, see EventDispatcher.addEventListener() in ActionScript 2.0 Components Language Reference.
For information about the events a component broadcasts, see each component's entry in the ActionScript 2.0 Components Language Reference.
// 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 |
|
CAUTION |
|
In a listener function, the keyword |
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00002451.html