Here are the first 10 paragraphs of Chapter 12 of Essential ActionScript 3.0
12. Events and Event Handling
In general terms, an event is a noteworthy runtime occurrence that has the potential to trigger a response in a program. In ActionScript, events can be broken into two categories: built-in events, which describe changes to the state of the runtime environment; and custom events, which describe changes to the state of a program. For example, a built-in event might be the clicking of the mouse or the completion of a file-load operation. By contrast, a custom event might be the ending of a game or the submission of an answer in a quiz.
Events are ubiquitous in ActionScript. In fact, in a pure ActionScript program, once the main-class constructor method has finished executing, all subsequent code is triggered by events. Accordingly, ActionScript supports a rich event architecture that provides the foundation for both built-in and custom events.
ActionScript’s event architecture is based on the W3C Document Object Model (DOM) Level 3 Events Specification, available at http://www.w3.org/TR/DOM-Level-3-Events.
This chapter teaches the fundamentals of ActionScript’s event architecture, covering both how to respond to built-in events and how to implement custom events in an ActionScript program. Note, however, that this chapter covers event fundamentals only. Later, in Chapter 21, Events and Display Hierarchies, we’ll study how ActionScript’s event architecture caters to display objects (objects that represent on-screen content). Then, in Chapter 22, Interactivity, we’ll examine a variety of specific built-in user-input events.
ActionScript Event Basics
In order to handle (respond to) events in an ActionScript program, we use event listeners. An event listener is a function or method that registers to be executed when a given event occurs. Event listeners are so named because they conceptually wait (listening) for events to happen. To notify a program that a given event has occurred, ActionScript executes any and all event listeners that have registered for that event. The notification process is known as an event dispatch.
When a given event dispatch is about to begin, ActionScript creates an object—known as the event object—that represents the event. The event object is always an instance of the Event class, or one of its descendants. All event listeners executed during the event dispatch are passed a reference to the event object as an argument. Each listener can use the event object’s variables to access information relating to the event. For example, a listener for an event representing mouse activity might use the variables of the event object to determine the location of the mouse pointer at the time of the event.
Every type of event in ActionScript—whether built-in or custom—is given a string name. For example, the name of the “mouse click” event-type is “click”. During an event dispatch, the name of the event being dispatched can be retrieved via the 'type' variable of the event object passed to every listener.
Each event dispatch in ActionScript has an event target, which is the object to which the event conceptually pertains. For example, for input events, the event target is typically the object that was manipulated (clicked on, typed into, moved over, etc.). Likewise, for network events, the event target is typically the object that instigated the network operation.
To respond to a given event, listeners typically register with the event target. Accordingly, all event target objects are instances of a class that inherits from the EventDispatcher class, or that implements the IEventDispatcher interface. The EventDispatcher class provides methods for registering and unregistering event listeners (addEventListener() and removeEventListener(), respectively).
In Chapter 21, we’ll learn that when the event target is a display object (an object that can be displayed on screen), event listeners can also register with the event target’s display ancestors (i.e., objects that visually contain the event target). For now, however, we’ll concentrate solely on non-displayable event-target objects.