6

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!

user648244
  • 1,240
  • 7
  • 26
  • 41

3 Answers3

20

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.
Feltope
  • 1,098
  • 6
  • 9
  • http://help.adobe.com/en_US/as2/reference/flashlite/WS5b3ccc516d4fbf351e63e3d118cd9b5f6e-7a54.html 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 – CoffeDeveloper May 10 '13 at 05:21
  • 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. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getTimer() – Feltope May 14 '13 at 07:34
  • you saved me headhackes :) – CoffeDeveloper May 16 '13 at 07:27
  • Your example is faulty: var myInt:int = getTimer() * 0.001; myInt will always be 0, because you cast an unassigned Number to an int – claudius iacob Jan 31 '17 at 20:32
1
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();
0
var countdown:Timer = new Timer(1000);
countdown.addEventListener(TimerEvent.TIMER, timerHandler);
countdown.start();

function timerHandler(e:TimerEvent):void
{           
    var minute = Math.floor(countdown.currentCount /  60);
    if(minute < 10)
        minute = '0'+minute;

    var second = countdown.currentCount % 60;
    if(second < 10)
        second = '0'+second;


    var timeElapsed = minute +':'+second;
    trace(timeElapsed);
}