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.

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

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

function ev<strong>en</strong>tResponse(ev<strong>en</strong>tObject:Ev<strong>en</strong>tType):void<br />

{<br />

// Actions performed in response to the ev<strong>en</strong>t go here.<br />

}<br />

ev<strong>en</strong>tTarget.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>tType.EVENT_NAME, ev<strong>en</strong>tResponse);<br />

This code does two things. First, it defines a function, which is the way to specify the actions that will be performed in<br />

response to the ev<strong>en</strong>t. Next, it calls the addEv<strong>en</strong>tList<strong>en</strong>er() method of the source object, in ess<strong>en</strong>ce “subscribing”<br />

the function to the specified ev<strong>en</strong>t so that wh<strong>en</strong> the ev<strong>en</strong>t happ<strong>en</strong>s, the function’s actions are carried out. Wh<strong>en</strong> the<br />

ev<strong>en</strong>t actually happ<strong>en</strong>s, the ev<strong>en</strong>t target checks its list of all the functions and methods that are registered as ev<strong>en</strong>t<br />

list<strong>en</strong>ers. It th<strong>en</strong> calls each one in turn, passing the ev<strong>en</strong>t object as a parameter.<br />

You need to alter four things in this code to create your own ev<strong>en</strong>t list<strong>en</strong>er. First, you must change the name of the<br />

function to the name you want to use (this must be changed in two places, where the code says ev<strong>en</strong>tResponse).<br />

Second, you must specify the appropriate class name of the ev<strong>en</strong>t object that is dispatched by the ev<strong>en</strong>t you want to<br />

list<strong>en</strong> for (Ev<strong>en</strong>tType in the code), and you must specify the appropriate constant for the specific ev<strong>en</strong>t<br />

(EVENT_NAME in the listing). Third, you must call the addEv<strong>en</strong>tList<strong>en</strong>er() method on the object that will<br />

dispatch the ev<strong>en</strong>t (ev<strong>en</strong>tTarget in this code). Optionally, you can change the name of the variable used as the<br />

function’s parameter (ev<strong>en</strong>tObject in this code).<br />

Important concepts and terms<br />

The following refer<strong>en</strong>ce list contains important terms that you will <strong>en</strong>counter wh<strong>en</strong> writing ev<strong>en</strong>t-handling routines:<br />

Bubbling Bubbling occurs for some ev<strong>en</strong>ts so that a par<strong>en</strong>t display object can respond to ev<strong>en</strong>ts dispatched by its<br />

childr<strong>en</strong>.<br />

Bubbling phase The part of the ev<strong>en</strong>t flow in which an ev<strong>en</strong>t propagates up to par<strong>en</strong>t display objects. The bubbling<br />

phase occurs after the capture and target phases.<br />

Capture phase The part of the ev<strong>en</strong>t flow in which an ev<strong>en</strong>t propagates down from the most g<strong>en</strong>eral target to the most<br />

specific target object. The capture phase occurs before the target and bubbling phases.<br />

Default behavior Some ev<strong>en</strong>ts include a behavior that normally happ<strong>en</strong>s along with the ev<strong>en</strong>t, known as the default<br />

behavior. For example, wh<strong>en</strong> a user types text in a text field, a text input ev<strong>en</strong>t is raised. The default behavior for that<br />

ev<strong>en</strong>t is to actually display the character that was typed into the text field—but you can override that default behavior<br />

(if for some reason you don’t want the typed character to be displayed).<br />

Dispatch To notify ev<strong>en</strong>t list<strong>en</strong>ers that an ev<strong>en</strong>t has occurred.<br />

Ev<strong>en</strong>t Something that happ<strong>en</strong>s to an object that the object can tell other objects about.<br />

Ev<strong>en</strong>t flow Wh<strong>en</strong> ev<strong>en</strong>ts happ<strong>en</strong> to an object on the display list (an object displayed on the scre<strong>en</strong>), all the objects that<br />

contain the object are notified of the ev<strong>en</strong>t and notify their ev<strong>en</strong>t list<strong>en</strong>ers in turn. This process starts with the Stage<br />

and proceeds through the display list to the actual object where the ev<strong>en</strong>t occurred, and th<strong>en</strong> proceeds back to the Stage<br />

again. This process is known as the ev<strong>en</strong>t flow.<br />

Ev<strong>en</strong>t object An object that contains information about a particular ev<strong>en</strong>t’s occurr<strong>en</strong>ce, which is s<strong>en</strong>t to all list<strong>en</strong>ers<br />

wh<strong>en</strong> an ev<strong>en</strong>t is dispatched.<br />

Ev<strong>en</strong>t target The object that actually dispatches an ev<strong>en</strong>t. For example, if the user clicks a button that is inside a Sprite<br />

that is in turn inside the Stage, all those objects dispatch ev<strong>en</strong>ts, but the ev<strong>en</strong>t target is the one where the ev<strong>en</strong>t actually<br />

happ<strong>en</strong>ed—in this case, the clicked button.<br />

List<strong>en</strong>er An object or function that has registered itself with an object, to indicate that it should be notified wh<strong>en</strong> a<br />

specific ev<strong>en</strong>t takes place.<br />

Last updated 6/6/2012<br />

126

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

Saved successfully!

Ooh no, something went wrong!