actionscript 3 how to keep track of time elapsed?

13,385

Solution 1

getTimer() will return an int of exactly how many milliseconds from when flash started.

import flash.utils.getTimer;

var myInt:int = getTimer() * 0.001;

myInt will now be however many seconds the program has been running.

edit: oh to tell how long it has been running just keep the initial myInt and check it against the current timer.

so when the game first starts.

var startTime:int = getTimer();

then every frame or whenever you need to check it.

var currentTime:int = getTimer();


var timeRunning:int = (currentTime - startTime) * 0.001; // this is how many seconds the game has been running.

Solution 2

var a:int = 0;

var onTimer:Function = function (e:TimerEvent):void {
    a += 2;
}

var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
Share:
13,385
user648244
Author by

user648244

Updated on June 14, 2022

Comments

  • user648244
    user648244 almost 2 years

    im new to actionscript3 flash. I have a int variable and i would like to add +2 every second since game started. How can i do this ? how do i know how much time has elapsed? thanks in advance!

  • CoffeDeveloper
    CoffeDeveloper about 11 years
    help.adobe.com/en_US/as2/reference/flashlite/… there is little ambiguity, I see in the IDE's getTimer returning int while flash documentation says getTimer returns Number. Would be usefull to know wich one is correct. cheers
  • Feltope
    Feltope almost 11 years
    the function declaration in the headers (library) is "public function getTimer():int" so it returns an int. by the way those are the actionScript 2 references not the actionScript 3 ones. help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/‌​…
  • CoffeDeveloper
    CoffeDeveloper almost 11 years
    you saved me headhackes :)
  • claudius iacob
    claudius iacob over 7 years
    Your example is faulty: var myInt:int = getTimer() * 0.001; myInt will always be 0, because you cast an unassigned Number to an int