13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Handling ev<strong>en</strong>ts<br />

Ev<strong>en</strong>t handling in ActionScript 3.0<br />

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

ActionScript 3.0 introduces a single ev<strong>en</strong>t-handling model that replaces the many differ<strong>en</strong>t ev<strong>en</strong>t-handling<br />

mechanisms that existed in previous versions of the language. The new ev<strong>en</strong>t model is based on the Docum<strong>en</strong>t Object<br />

Model (DOM) Level 3 Ev<strong>en</strong>ts Specification. Although the SWF file format does not adhere specifically to the<br />

Docum<strong>en</strong>t Object Model standard, there are suffici<strong>en</strong>t similarities betwe<strong>en</strong> the display list and the structure of the<br />

DOM to make implem<strong>en</strong>tation of the DOM ev<strong>en</strong>t model possible. An object on the display list is analogous to a node<br />

in the DOM hierarchical structure, and the terms display list object and node are used interchangeably throughout this<br />

discussion.<br />

The Flash Player and AIR implem<strong>en</strong>tation of the DOM ev<strong>en</strong>t model includes a concept named default behaviors. A<br />

default behavior is an action that Flash Player or AIR executes as the normal consequ<strong>en</strong>ce of certain ev<strong>en</strong>ts.<br />

Default behaviors<br />

Developers are usually responsible for writing code that responds to ev<strong>en</strong>ts. In some cases, however, a behavior is so<br />

commonly associated with an ev<strong>en</strong>t that Flash Player or AIR automatically executes the behavior unless the developer<br />

adds code to cancel it. Because Flash Player or AIR automatically exhibits the behavior, such behaviors are called<br />

default behaviors.<br />

For example, wh<strong>en</strong> a user <strong>en</strong>ters text into a TextField object, the expectation that the text will be displayed in that<br />

TextField object is so common that the behavior is built into Flash Player and AIR. If you do not want this default<br />

behavior to occur, you can cancel it using the new ev<strong>en</strong>t-handling system. Wh<strong>en</strong> a user inputs text into a TextField<br />

object, Flash Player or AIR creates an instance of the TextEv<strong>en</strong>t class to repres<strong>en</strong>t that user input. To prev<strong>en</strong>t Flash<br />

Player or AIR from displaying the text in the TextField object, you must access that specific TextEv<strong>en</strong>t instance and<br />

call that instance’s prev<strong>en</strong>tDefault() method.<br />

Not all default behaviors can be prev<strong>en</strong>ted. For example, Flash Player and AIR g<strong>en</strong>erate a MouseEv<strong>en</strong>t object wh<strong>en</strong> a<br />

user double-clicks a word in a TextField object. The default behavior, which cannot be prev<strong>en</strong>ted, is that the word<br />

under the cursor is highlighted.<br />

Many types of ev<strong>en</strong>t objects do not have associated default behaviors. For example, Flash Player dispatches a connect<br />

ev<strong>en</strong>t object wh<strong>en</strong> a network connection is established, but there is no default behavior associated with it. The API<br />

docum<strong>en</strong>tation for the Ev<strong>en</strong>t class and its subclasses lists each type of ev<strong>en</strong>t and describes any associated default<br />

behavior, and whether that behavior can be prev<strong>en</strong>ted.<br />

It is important to understand that default behaviors are associated only with ev<strong>en</strong>t objects dispatched by Flash Player<br />

or AIR, and do not exist for ev<strong>en</strong>t objects dispatched programmatically through ActionScript. For example, you can<br />

use the methods of the Ev<strong>en</strong>tDispatcher class to dispatch an ev<strong>en</strong>t object of type textInput, but that ev<strong>en</strong>t object will<br />

not have a default behavior associated with it. In other words, Flash Player and AIR will not display a character in a<br />

TextField object as a result of a textInput ev<strong>en</strong>t that you dispatched programmatically.<br />

What’s new for ev<strong>en</strong>t list<strong>en</strong>ers in ActionScript 3.0<br />

For developers with experi<strong>en</strong>ce using the ActionScript 2.0 addList<strong>en</strong>er() method, it may be helpful to point out the<br />

differ<strong>en</strong>ces betwe<strong>en</strong> the ActionScript 2.0 ev<strong>en</strong>t list<strong>en</strong>er model and the ActionScript 3.0 ev<strong>en</strong>t model. The following list<br />

describes a few major differ<strong>en</strong>ces betwe<strong>en</strong> the two ev<strong>en</strong>t models:<br />

To add ev<strong>en</strong>t list<strong>en</strong>ers in ActionScript 2.0, you use addList<strong>en</strong>er() in some cases and addEv<strong>en</strong>tList<strong>en</strong>er() in<br />

others, whereas in ActionScript 3.0, you use addEv<strong>en</strong>tList<strong>en</strong>er() in all situations.<br />

There is no ev<strong>en</strong>t flow in ActionScript 2.0, which means that the addList<strong>en</strong>er() method can be called only on the<br />

object that broadcasts the ev<strong>en</strong>t, whereas in ActionScript 3.0, the addEv<strong>en</strong>tList<strong>en</strong>er() method can be called on<br />

any object that is part of the ev<strong>en</strong>t flow.<br />

Last updated 6/6/2012<br />

128

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

Saved successfully!

Ooh no, something went wrong!