Examining the event-handling process

The following is a step-by-step description of the process that happens when you create an event listener. In this case, it's an example of creating a listener function that is called when an object named myButton is clicked.

The actual code written by the programmer is as follows:

function eventResponse(event:MouseEvent):void
{
    // Actions performed in response to the event go here.
}

myButton.addEventListener(MouseEvent.CLICK, eventResponse);

Here is how this code would actually work when it's running in Flash Player:

  1. When the SWF file loads, Flash Player makes note of the fact that there's a function named eventResponse().


    A representation of Flash Player becoming aware of a function named "eventResponse()"

  2. Flash Player then runs the code (specifically, the lines of code that aren't in a function). In this case that's only one line of code: calling the addEventListener() method on the event source object (named myButton) and passing the eventResponse function as a parameter.


    A representation of Flash Player calling the addEventListener() method of the object myButton, subscribing eventResponse() as a listener.

    1. Internally, myButton has a list of functions that are listening to each of its events, so when its addEventListener() method is called, myButton stores the eventResponse() function in its list of event listeners.


      A representation of Flash Player storing eventResponse() in myButton's internal list of listeners

  3. At some point, the user clicks the myButton object, triggering its click event (identified as MouseEvent.CLICK in the code).


    A depiction of a user's mouse cursor clicking on the button myButton

    At that point, the following occurs:

    1. Flash Player creates an object, an instance of the class associated with the event in question (MouseEvent in this example). For many events this will be an instance of the Event class; for mouse events it will be a MouseEvent instance; and for other events it will be an instance of the class that's associated with that event. This object that's created is known as the event object, and it contains specific information about the event that happened: what type of event it is, where it happened, and other event-specific information if applicable.


      A representation of Flash Player creating a new object "eventObject"

    2. Flash Player then looks at the list of event listeners stored by myButton. It goes through these functions one by one, calling each function and passing the event object to the function as a parameter. Since the eventResponse() function is one of myButton's listeners, as part of this process Flash Player calls the eventResponse() function.


      A representation of Flash Player (specifically, myButton) calling the function eventResponse(), passing a reference to eventObject as a parameter

    3. When the eventResponse() function is called, the code in that function runs, so your specified actions are carried out.


      A representation of Flash Player carring out the actions contained in the function "eventResponse()"


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/00000022.html