如何让计时器倒数AS3

时间:2019-07-19 23:18:22

标签: actionscript-3 flash

我有一个计时器,试图将其倒数而不是倒数。几年前,我使用了这段代码,似乎无法弄清楚如何从5分钟开始倒数。

我只尝试弄乱变量。

这是我的变量,等等:

tCountTimer = new Timer(100);
        tCountTimer.addEventListener(TimerEvent.TIMER, timerTickHandler);
        timerCount = 0;

        tCountTimer.start();

这是我要数的功能:

private function timerTickHandler(e:Event):void 
    {
         timerCount += 100;
         toTimeCode(timerCount);
    }

    private function toTimeCode(milliseconds:int):void 
    {
        this.milliseconds = milliseconds;


        //create a date object using the elapsed milliseconds
        time = new Date(milliseconds);

        //define minutes/seconds/mseconds
        minutes = String(time.minutes + 5);
        seconds = String(time.seconds);
        miliseconds = String(Math.round(time.milliseconds) / 100);


        //add zero if neccecary, for example: 2:3.5 becomes 02:03.5
        minutes = (minutes.length != 2) ? '0'+ minutes : minutes;
        seconds = (seconds.length != 2) ? '0' + seconds : seconds;


        //display elapsed time on in a textfield on stage
        playScreen.timeLimitTextField.text = minutes + ":" + seconds;


    }

它算起来很完美,但不能让它从5分钟开始倒计时。感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

没有测试,但是我希望这个想法绝对清楚。

// Remember the time (in milliseconds) in 5 minutes from now.
var endTime:int = getTimer() + 5 * 60 * 1000;

// Call update function each frame.
addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(e:Event):void
{
    // How many milliseconds left to target time.
    var aTime:int = endTime - getTimer();

    // Fix if we are past target time.
    if (aTime < 0) aTime = 0;

    // Convert remaining time from milliseconds to seconds.
    aTime /= 1000;

    // Convert the result into text:
    // aTime % 60 = seconds (with minutes stripped off)
    // aTime / 60 = full minutes left
    var aText:String = ze(aTime / 60) + ":" + ze(aTime % 60);

    // Do whatever you want with it.
    trace(aText);
}

// Function to convert int to String
// and add a leading zero if necessary.
function ze(value:int):String
{
    return ((value < 10)? "0": "") + value.toString();
}