0
votes

im doing a basic game in action script and now i want to do a timer. I want that the timer starts count when the game starts and in the end of the game when the player can do ten points i want to say in textfield that if the time was more than 5 minutes it was very bad if the timer was less than 2minutes very good and things like this! Im trying do this but the timer dont count, anyone can help? Thanks!

theTime.addEventListener(Event.ENTER_FRAME,showTime);

function showTime(event:Event):void {

var myTime:Date = new Date();

var theMinutes=myTime.getMinutes();

 theTime.text =theMinutes;
} 
1

1 Answers

0
votes

new Date(); gives a Date object which contains the current date and time. To keep track of passed time you need to keep track of start and end time and find their difference. You can do this by using time property. Something like this:

// Do this when you start the game.
var startTime:Number = (new Date()).time;

// Do this when the game is over
var endTime:Number = (new Date()).time;

const MILLI_SECOND_IN_5_MIN:Number = 5 * 60 * 1000;
const MILLI_SECOND_IN_2_MIN:Number = 5 * 60 * 1000;

var timeDiff:Number = endTime - startTime;

if (timeDiff < MILLI_SECOND_IN_2_MIN) {
    trace("Good");
} else if (timeDiff > MILLI_SECOND_IN_5_MIN) {
    trace("Bad");
}