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 />

Managing ev<strong>en</strong>t list<strong>en</strong>ers<br />

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

You can manage your list<strong>en</strong>er functions using the methods of the IEv<strong>en</strong>tDispatcher interface. The IEv<strong>en</strong>tDispatcher<br />

interface is the ActionScript 3.0 version of the Ev<strong>en</strong>tTarget interface of the DOM ev<strong>en</strong>t model. Although the name<br />

IEv<strong>en</strong>tDispatcher may seem to imply that its main purpose is to s<strong>en</strong>d (or dispatch) ev<strong>en</strong>t objects, the methods of this<br />

class are actually used much more frequ<strong>en</strong>tly to register ev<strong>en</strong>t list<strong>en</strong>ers, check for ev<strong>en</strong>t list<strong>en</strong>ers, and remove ev<strong>en</strong>t<br />

list<strong>en</strong>ers. The IEv<strong>en</strong>tDispatcher interface defines five methods, as shown in the following code:<br />

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

{<br />

public interface IEv<strong>en</strong>tDispatcher<br />

{<br />

function addEv<strong>en</strong>tList<strong>en</strong>er(ev<strong>en</strong>tName:String,<br />

list<strong>en</strong>er:Object,<br />

useCapture:Boolean=false,<br />

priority:Integer=0,<br />

useWeakRefer<strong>en</strong>ce:Boolean=false):Boolean;<br />

}<br />

}<br />

function removeEv<strong>en</strong>tList<strong>en</strong>er(ev<strong>en</strong>tName:String,<br />

list<strong>en</strong>er:Object,<br />

useCapture:Boolean=false):Boolean;<br />

function dispatchEv<strong>en</strong>t(ev<strong>en</strong>tObject:Ev<strong>en</strong>t):Boolean;<br />

function hasEv<strong>en</strong>tList<strong>en</strong>er(ev<strong>en</strong>tName:String):Boolean;<br />

function willTrigger(ev<strong>en</strong>tName:String):Boolean;<br />

The Flash Player API implem<strong>en</strong>ts the IEv<strong>en</strong>tDispatcher interface with the Ev<strong>en</strong>tDispatcher class, which serves as a<br />

base class for all classes that can be ev<strong>en</strong>t targets or part of an ev<strong>en</strong>t flow. For example, the DisplayObject class inherits<br />

from the Ev<strong>en</strong>tDispatcher class. This means that any object on the display list has access to the methods of the<br />

IEv<strong>en</strong>tDispatcher interface.<br />

Adding ev<strong>en</strong>t list<strong>en</strong>ers<br />

The addEv<strong>en</strong>tList<strong>en</strong>er() method is the workhorse of the IEv<strong>en</strong>tDispatcher interface. You use it to register your<br />

list<strong>en</strong>er functions. The two required parameters are type and list<strong>en</strong>er. You use the type parameter to specify the<br />

type of ev<strong>en</strong>t. You use the list<strong>en</strong>er parameter to specify the list<strong>en</strong>er function that will execute wh<strong>en</strong> the ev<strong>en</strong>t occurs.<br />

The list<strong>en</strong>er parameter can be a refer<strong>en</strong>ce to either a function or a class method.<br />

Do not use par<strong>en</strong>theses wh<strong>en</strong> you specify the list<strong>en</strong>er parameter. For example, the clickHandler() function is<br />

specified without par<strong>en</strong>theses in the following call to the addEv<strong>en</strong>tList<strong>en</strong>er() method:<br />

addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, clickHandler)<br />

The useCapture parameter of the addEv<strong>en</strong>tList<strong>en</strong>er() method allows you to control the ev<strong>en</strong>t flow phase on which<br />

your list<strong>en</strong>er will be active. If useCapture is set to true, your list<strong>en</strong>er will be active during the capture phase of the<br />

ev<strong>en</strong>t flow. If useCapture is set to false, your list<strong>en</strong>er will be active during the target and bubbling phases of the ev<strong>en</strong>t<br />

flow. To list<strong>en</strong> for an ev<strong>en</strong>t during all phases of the ev<strong>en</strong>t flow, you must call addEv<strong>en</strong>tList<strong>en</strong>er() twice, once with<br />

useCapture set to true, and th<strong>en</strong> again with useCapture set to false.<br />

Last updated 6/6/2012<br />

138

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

Saved successfully!

Ooh no, something went wrong!