动作脚本3.0。重新定时器到倒数计时器

时间:2015-03-29 12:09:26

标签: actionscript-3 flash timer countdown

我有计时器,以mm:ss从以下格式计算时间00:00。我需要制作从30

开始的倒数计时器

我的计时器如下:

public class Test extends Sprite {

        var timer:Timer; 
        var txtTime:TextField;
        var tmpTime:Number;  //this will store the time when the game is started 
}

function game(e:MouseEvent) {
            timer = new Timer(0); //create a new timer that ticks every second.
            timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true);              //listen for the timer tick
            timer.addEventListener(TimerEvent.TIMER, resetTimer);
            txtTime = new TextField();
            txtTime.text = setNull(0);

            var format:TextFormat = new TextFormat();
            format.color = 0x000000;
            format.size = 22;
            format.bold = true;
            txtTime.defaultTextFormat = format;
            txtTime.x = 603;
            txtTime.y = 143;
            addChild(txtTime);

            timer.reset();

            tmpTime = flash.utils.getTimer();
            timer.start();
}

        function setNull(startTime:int):String
        {
            return "00:00";
        }
        private function tick(e:Event):void {
            txtTime.text = showTimePassed(tmpTime);
        }
        function showTimePassed(startTime:int):String {
            var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
            var leadingZeroM:String = "";
            var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
            var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds

            if (seconds < 10) { //if seconds are less than two digits, add the leading zero
                leadingZeroS = "0";
            }       
            if (seconds < 600) { //if seconds are less than two digits, add the leading zero
                leadingZeroM = "0";
            }       
            var minutes = Math.floor( (time / (60 * 1000) ) ); //60 seconds times 1000 miliseocnds gets the minutes
            return leadingZeroM + minutes + ":" + leadingZeroS + seconds;
        }
        public function resetTimer(event:TimerEvent):void
        {
            var timer:Timer = event.target as Timer;
            timer.reset();
            timer.start();
        }
        function getTimePassed(startTime:int):Number
        {
            return (getTimer() - startTime) /1000;
        }

我无法成功将它从30秒改为倒计时。

我试图在以下内容中声明计时器:timer = new Timer(1000, 30); 并更改var time = 30 - getTimer();而不是var time = getTimer() - startTime;,但它不起作用。

你有想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

要创建倒计时,您可以使用Timer.currentCount,如下所示:

var time:int = 0;
var timer:Timer = new Timer(1000, 30)
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    function onTimer(e:TimerEvent):void {
        time = timer.repeatCount - timer.currentCount;
        trace('00:' + (time < 10 ? '0' + time : time)); // gives : 00:29, 00:28, .., 00:01, 00:00
    }
    timer.start();

trace('00:' + (timer.repeatCount - time));              //  gives : 00:30

希望可以提供帮助。