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

Create successful ePaper yourself

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

Chapter 8: Handling ev<strong>en</strong>ts<br />

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

An ev<strong>en</strong>t-handling system allows programmers to respond to user input and system ev<strong>en</strong>ts in a conv<strong>en</strong>i<strong>en</strong>t way. The<br />

ActionScript 3.0 ev<strong>en</strong>t model is not only conv<strong>en</strong>i<strong>en</strong>t, but also standards-compliant, and well integrated with the<br />

display list. Based on the Docum<strong>en</strong>t Object Model (DOM) Level 3 Ev<strong>en</strong>ts Specification, an industry-standard ev<strong>en</strong>thandling<br />

architecture, the new ev<strong>en</strong>t model provides a powerful yet intuitive ev<strong>en</strong>t-handling tool for ActionScript<br />

programmers.<br />

The ActionScript 3.0 ev<strong>en</strong>t-handling system interacts closely with the display list. To gain a basic understanding of the<br />

display list, read “Display programming” on page 151.<br />

More Help topics<br />

flash.ev<strong>en</strong>ts package<br />

Docum<strong>en</strong>t Object Model (DOM) Level 3 Ev<strong>en</strong>ts Specification<br />

Basics of handling ev<strong>en</strong>ts<br />

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

You can think of ev<strong>en</strong>ts as occurr<strong>en</strong>ces of any kind in your SWF file that are of interest to you as a programmer. For<br />

example, most SWF files support user interaction of some sort—whether it's something as simple as responding to a<br />

mouse click or something more complex, such as accepting and processing data <strong>en</strong>tered into a form. Any such user<br />

interaction with your SWF file is considered an ev<strong>en</strong>t. Ev<strong>en</strong>ts can also occur without any direct user interaction, such<br />

as wh<strong>en</strong> data has finished loading from a server or wh<strong>en</strong> an attached camera has become active.<br />

In ActionScript 3.0, each ev<strong>en</strong>t is repres<strong>en</strong>ted by an ev<strong>en</strong>t object, which is an instance of the Ev<strong>en</strong>t class or one of its<br />

subclasses. An ev<strong>en</strong>t object not only stores information about a specific ev<strong>en</strong>t, but also contains methods that facilitate<br />

manipulation of the ev<strong>en</strong>t object. For example, wh<strong>en</strong> Flash Player or AIR detects a mouse click, it creates an ev<strong>en</strong>t<br />

object (an instance of the MouseEv<strong>en</strong>t class) to repres<strong>en</strong>t that particular mouse click ev<strong>en</strong>t.<br />

After creating an ev<strong>en</strong>t object, Flash Player or AIR dispatches it, which means that the ev<strong>en</strong>t object is passed to the<br />

object that is the target of the ev<strong>en</strong>t. An object that serves as the destination for a dispatched ev<strong>en</strong>t object is called an<br />

ev<strong>en</strong>t target. For example, wh<strong>en</strong> an attached camera becomes active, Flash Player dispatches an ev<strong>en</strong>t object directly to<br />

the ev<strong>en</strong>t target, which in this case is the object that repres<strong>en</strong>ts the camera. If the ev<strong>en</strong>t target is on the display list,<br />

however, the ev<strong>en</strong>t object is passed down through the display list hierarchy until it reaches the ev<strong>en</strong>t target. In some<br />

cases, the ev<strong>en</strong>t object th<strong>en</strong> “bubbles” back up the display list hierarchy along the same route. This traversal of the<br />

display list hierarchy is called the ev<strong>en</strong>t flow.<br />

You can “list<strong>en</strong>” for ev<strong>en</strong>t objects in your code using ev<strong>en</strong>t list<strong>en</strong>ers. Ev<strong>en</strong>t list<strong>en</strong>ers are the functions or methods that<br />

you write to respond to specific ev<strong>en</strong>ts. To <strong>en</strong>sure that your program responds to ev<strong>en</strong>ts, you must add ev<strong>en</strong>t list<strong>en</strong>ers<br />

either to the ev<strong>en</strong>t target or to any display list object that is part of an ev<strong>en</strong>t object’s ev<strong>en</strong>t flow.<br />

Any time you write ev<strong>en</strong>t list<strong>en</strong>er code, it follows this basic structure (elem<strong>en</strong>ts in bold are placeholders you’d fill in<br />

for your specific case):<br />

Last updated 6/6/2012<br />

125

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

Saved successfully!

Ooh no, something went wrong!