actionscript 3 how to keep track of time elapsed?

前端 未结 3 952
无人共我
无人共我 2021-01-05 13:36

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?

相关标签:
3条回答
  • 2021-01-05 14:10

    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.
    
    0 讨论(0)
  • 2021-01-05 14:15
    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);
    }
    
    0 讨论(0)
  • 2021-01-05 14:19
    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 讨论(0)
提交回复
热议问题