29.04.2013 Views

ACTIONSCRIPT 3.0

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> <strong>3.0</strong> DEVELOPER’S GUIDE<br />

Handling events<br />

If a user clicks on Child1 Node, Flash Player or AIR dispatches an event object into the event flow. As the following<br />

image shows, the object’s journey starts at Stage, moves down to Parent Node, then moves to Child1 Node, and<br />

then “bubbles” back up to Stage, moving through Parent Node again on its journey back to Stage.<br />

Capture<br />

Phase<br />

Stage<br />

Parent Node<br />

Child1 Node Child2 Node<br />

Target Phase<br />

In this example, the capture phase comprises Stage and Parent Node during the initial downward journey. The target<br />

phase comprises the time spent at Child1 Node. The bubbling phase comprises Parent Node and Stage as they are<br />

encountered during the upward journey back to the root node.<br />

The event flow contributes to a more powerful event-handling system than that previously available to ActionScript<br />

programmers. In previous versions of ActionScript, the event flow does not exist, which means that event listeners can<br />

be added only to the object that generates the event. In ActionScript <strong>3.0</strong>, you can add event listeners not only to a target<br />

node, but also to any node along the event flow.<br />

The ability to add event listeners along the event flow is useful when a user interface component comprises more than<br />

one object. For example, a button object often contains a text object that serves as the button’s label. Without the ability<br />

to add a listener to the event flow, you would have to add a listener to both the button object and the text object to<br />

ensure that you receive notification about click events that occur anywhere on the button. The existence of the event<br />

flow, however, allows you to place a single event listener on the button object that handles click events that occur either<br />

on the text object or on the areas of the button object that are not obscured by the text object.<br />

Not every event object, however, participates in all three phases of the event flow. Some types of events, such as the<br />

enterFrame and init event types, are dispatched directly to the target node and participate in neither the capture<br />

phase nor the bubbling phase. Other events may target objects that are not on the display list, such as events dispatched<br />

to an instance of the Socket class. These event objects will also flow directly to the target object, without participating<br />

in the capture and bubbling phases.<br />

To find out how a particular event type behaves, you can either check the API documentation or examine the event<br />

object's properties. Examining the event object’s properties is described in the following section.<br />

Event objects<br />

Bubbling<br />

Phase<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Event objects serve two main purposes in the new event-handling system. First, event objects represent actual events<br />

by storing information about specific events in a set of properties. Second, event objects contain a set of methods that<br />

allow you to manipulate event objects and affect the behavior of the event-handling system.<br />

To facilitate access to these properties and methods, the Flash Player API defines an Event class that serves as the base<br />

class for all event objects. The Event class defines a fundamental set of properties and methods that are common to all<br />

event objects.<br />

Last updated 4/22/2013<br />

130

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!