moock.org is supported in part by


April 19, 2007

Chapter 21, Paragraphs 1-6, Essential ActionScript 3.0

Here are the first 6 paragraphs of Chapter 21 of Essential ActionScript 3.0

21. Events and Display Hierarchies

In Chapter 12, Events and Event Handling, we studied ActionScript’s built-in event architecture in general terms. In this chapter, we’ll take a closer look at how that event architecture specifically caters to objects in display hierarchies.

ActionScript’s system of dispatching events through an object hierarchy, as described in this chapter, is based on the W3C Document Object Model (DOM) Level 3 Events Specification, available at http://www.w3.org/TR/DOM-Level-3-Events.

Hierarchical Event Dispatch

As we saw in Chapter 12, when ActionScript dispatches an event targeted at an object that is not part of a display hierarchy, that target is the sole object notified of the event. For example, when a Sound object’s sound finishes playing, ActionScript dispatches an Event.COMPLETE event targeted at the associated SoundChannel object. The SoundChannel object is not part of a display hierarchy, so it is the sole object notified that the event occurred.

By contrast, when ActionScript dispatches an event targeted at an object that is part of a display hierarchy, that target and all of its display hierarchy ancestors are notified that the event occurred. For example, if a Sprite object contains a TextField object, and the user clicks the TextField object, both the TextField object (the event target) and the Sprite object (the event target’s ancestor) are notified that the mouse click occurred.

ActionScript’s hierarchical event dispatch system gives every display object container the ability to register event listeners that handle events targeted at its descendant display objects. For example, a Sprite representing a dialog box might register a listener that handles mouse click events targeted at a nested “OK button” control. Or a Sprite representing a login form might register a listener that handles focus events targeted at nested input fields. This centralized architecture helps reduce code repetition, particularly for code that responds to user input events. Later, under “Using the Event Flow to Centralize Code,” we’ll study a code example that demonstrates the benefits of centralized event handling. But first, let’s cover the basics of hierarchical event dispatch and registration.

In this chapter, the terms “ancestor” and “descendant” refer primarily to objects in a display hierarchy, not to superclasses and subclasses in an inheritance hierarchy. To avoid confusion, this chapter sometimes uses the informal terms “display ancestor” and “display descendant” when referring to ancestor objects and descendant objects in a display hierarchy.

Posted by moock at April 19, 2007 09:08 PM