O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the Event Flow to Centralize Code

If you're waiting for room in a fully booked hotel, it's easier to ask the hotel manager to tell you when a vacancy opens up than it is to ask every guest in the hotel to tell you when they leave. Likewise, when handling event dispatches, it's often more efficient to register event listeners with a display object container than it is to register with each of its descendants.

For example, suppose we're building a simple checkbox control, comprised of the following two classes:

  • CheckBox, a Sprite subclass that acts as a container for the entire control

  • CheckBoxIcon, a Sprite subclass that represents the checkbox's graphical icon

At runtime, each CheckBox instance creates two child objects: a CheckBoxIcon instance for the checkbox's icon and a TextField instance for the checkbox's text label. For reference, let's call the main CheckBox instance container and its two children icon and label. Figure 21-3 shows our checkbox control.

Objects in the Checkbox control

Figure 21-3. Objects in the Checkbox control

We want our checkbox to be easy to use, so we design it to toggle on or off when the user clicks either the checkbox icon or the checkbox label. Accordingly, in our implementation, we must detect mouse-click events targeted at both icon and label. To do so, we could register a separate mouse-click listener with each of those objects. However, registering two event listeners would ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required