|
Adding a Timer
In this tutorial I will show you how to create a timer. To keep things consistant I will provide an example of by puting a textbox on the stage and giving it an instance name of "timeString". Then give it some variables and actions of how to increment the timer. The output will look like this Time: 1:35.
var startTime:int = getTimer();
addEventListener(Event.ENTER_FRAME, showClock);
function showClock(event:Event){
//ms passed
var timePassed:int = getTimer()-startTime;
//computer minutes and seconds
var seconds:int = Math.floor(timePassed/1000);
var minutes:int = Math.floor(seconds/60);
seconds -= minutes*60;
//convert clock to string
var timeString:String = "Timer " + minutes + " : " + String(seconds + 100).substr(1,2);
//show in text field'
timeDisplay.text = timeString;
}

This code basically calls a couple functions and executes a few lines of code that does an equation of what needs to be done to make a propper game time string.
|