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:

function myFunction:Function(evtObj:Object){
    // your code here 
}

TIP

 

The evtObj parameter is an object that is automatically generated when an event is triggered and passed to the function. The event object has properties that contain information about the event. For details, see About the event object.

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.

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 List component to the Stage from the Components panel.
  3. In the Property inspector, enter the instance name myList.
  4. Select Frame 1 in the Timeline.
  5. Select Window > 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. To see an example of function scoping, see About scope in listeners.


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