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

Working with dates and times<br />

package<br />

{<br />

import flash.display.Sprite;<br />

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

import flash.utils.Timer;<br />

}<br />

public class ShortTimer ext<strong>en</strong>ds Sprite<br />

{<br />

public function ShortTimer()<br />

{<br />

// creates a new five-second Timer<br />

var minuteTimer:Timer = new Timer(1000, 5);<br />

}<br />

}<br />

// designates list<strong>en</strong>ers for the interval and completion ev<strong>en</strong>ts<br />

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

minuteTimer.addEv<strong>en</strong>tList<strong>en</strong>er(TimerEv<strong>en</strong>t.TIMER_COMPLETE, onTimerComplete);<br />

// starts the timer ticking<br />

minuteTimer.start();<br />

public function onTick(ev<strong>en</strong>t:TimerEv<strong>en</strong>t):void<br />

{<br />

// displays the tick count so far<br />

// The target of this ev<strong>en</strong>t is the Timer instance itself.<br />

trace("tick " + ev<strong>en</strong>t.target.curr<strong>en</strong>tCount);<br />

}<br />

public function onTimerComplete(ev<strong>en</strong>t:TimerEv<strong>en</strong>t):void<br />

{<br />

trace("Time's Up!");<br />

}<br />

Wh<strong>en</strong> the ShortTimer class is created, it creates a Timer instance that will tick once per second for five seconds. Th<strong>en</strong><br />

it adds two list<strong>en</strong>ers to the timer: one that list<strong>en</strong>s to each tick, and one that list<strong>en</strong>s for the timerComplete ev<strong>en</strong>t.<br />

Next, it starts the timer ticking, and from that point forward, the onTick() method executes at one-second intervals.<br />

The onTick() method simply displays the curr<strong>en</strong>t tick count. After five seconds have passed, the<br />

onTimerComplete() method executes, telling you that the time is up.<br />

Wh<strong>en</strong> you run this sample, you should see the following lines appear in your console or trace window at the rate of<br />

one line per second:<br />

tick 1<br />

tick 2<br />

tick 3<br />

tick 4<br />

tick 5<br />

Time's Up!<br />

Last updated 6/6/2012<br />

5

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

Saved successfully!

Ooh no, something went wrong!