Creating a Timer

Creating a Timer

Flash CS4 ActionScript 3.0 Tutorial

 

How to Count Time Elapsed in a Flash SWF Movie

 

 

Displaying time elapsed is handy for a game or time-based quiz. The getTimer() function starts counting once your swf file loads. In this example, we query the start time and use that value to calculate the time since the start button has been pressed. The calculated value is displayed and updated at the file's frame rate.

Tutorial Elements

Button Symbol with instance name, red_btn

Button Symbol with instance name, green_btn

Dynamic text object with instance name, outText_txt

 

Step One

Open a new Flash document and name the default layer, Content.

Step Two

Add a Button Symbol with an instance name of red_btn. For more information on creating buttons, see topic, Button Symbol Basics.

Step Three

Add another Button Symbol with an instance name of green_btn.

Step Four

Add an empty Dynamic text object an instance name of outText_txt. For more information on creating Dynamic text objects, see topic, Dynamic Text.

Step Five

Add a new layer called Actions.

Step Six

Option double-click (PC - Alt double-click) the keyframe in the Actions layer to open the ActionScript panel.

Step Seven

Copy/Paste the following code to the ActionScript panel.

var totalSec:int=0;

var timeStart:int=0;

var timeCurrent:int=0;

outText_txt.visible=false;

addEventListener(Event.ENTER_FRAME, traceTimer);

function traceTimer(e:Event):void {

timeCurrent=getTimer()-timeStart;

totalSec=(timeCurrent/1000);

outText_txt.text=String(totalSec);

}

green_btn.addEventListener(MouseEvent.CLICK, startTimer);

red_btn.addEventListener(MouseEvent.CLICK, stopTimer);

function startTimer(e:MouseEvent):void {

timeStart = getTimer();

outText_txt.visible=true;

}

function stopTimer(e:Event):void {

outText_txt.visible=false;

}

Step Eight

Click the Auto Format icon to format the code.

Step Nine

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). Press the green start button to start or reset your timer. The red button stops the timer.

Dissecting the Code

The first three lines of the code establish three integer variables named totalSec, timeStart, and timeCurrent. Each variable is set to zero. The outText_txt field is set to invisible.

An event listener is added that accesses the traceTimer function. The traceTimer function calculates the time elapsed by subtracting the start time from the current time. The value is then converted from milliseconds to seconds by dividing by 1,000.

To display the calculated value in the Dynamic text field, it must be interpreted as a string, so the String(totalSec) conversion is added.

The next two lines add an event listener to the two buttons, green_btn and red_btn. When the buttons are pressed, the functions, startTimer and stopTimer, respectively, are called.

When the startTimer function is called, it uses the getTimer() function to read the time elapsed since the swf file opened. That value is assigned to the timeStart variable. The outText_txt field is made visible

The stopTimer function hides the outText_txt field.

 

Shutterfly 50 Free Prints 160x600