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

import flash.ev<strong>en</strong>ts.TimerEv<strong>en</strong>t;<br />

import flash.utils.Timer;<br />

/**<br />

* The Timer that will be used for the alarm.<br />

*/<br />

public var alarmTimer:Timer;<br />

...<br />

/**<br />

* Instantiates a new AlarmClock of a giv<strong>en</strong> size.<br />

*/<br />

public override function initClock(faceSize:Number = 200):void<br />

{<br />

super.initClock(faceSize);<br />

alarmTimer = new Timer(0, 1);<br />

alarmTimer.addEv<strong>en</strong>tList<strong>en</strong>er(TimerEv<strong>en</strong>t.TIMER, onAlarm);<br />

}<br />

The Timer instance defined in the AlarmClock class is named alarmTimer. The initClock() method, which<br />

performs necessary setup operations for the AlarmClock instance, does two things with the alarmTimer variable.<br />

First, the variable is instantiated with parameters instructing the Timer instance to wait 0 milliseconds and only trigger<br />

its timer ev<strong>en</strong>t one time. After instantiating alarmTimer, the code calls that variable’s addEv<strong>en</strong>tList<strong>en</strong>er() method<br />

to indicate that it wants to list<strong>en</strong> to that variable’s timer ev<strong>en</strong>t. A Timer instance works by dispatching its timer ev<strong>en</strong>t<br />

after a specified amount of time has passed. The AlarmClock class will need to know wh<strong>en</strong> the timer ev<strong>en</strong>t is<br />

dispatched in order to set off its own alarm. By calling addEv<strong>en</strong>tList<strong>en</strong>er(), the AlarmClock code registers itself as<br />

a list<strong>en</strong>er with alarmTimer. The two parameters indicate that the AlarmClock class wants to list<strong>en</strong> for the timer ev<strong>en</strong>t<br />

(indicated by the constant TimerEv<strong>en</strong>t.TIMER), and that wh<strong>en</strong> the ev<strong>en</strong>t happ<strong>en</strong>s, the AlarmClock class’s onAlarm()<br />

method should be called in response to the ev<strong>en</strong>t.<br />

In order to actually set the alarm, the AlarmClock class’s setAlarm() method is called, as follows:<br />

Last updated 6/6/2012<br />

142

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

Saved successfully!

Ooh no, something went wrong!