as3倒计时器,设置不同的起始量

时间:2013-05-23 18:04:03

标签: actionscript-3

嘿伙计我是动作脚本中的新手3这是我的代码,任何人都可以帮我弄清楚如何让它重新恢复到每轮或每级的原始起始时间。

这是我的变量

private var seconds:int = 60;
private var minutes:int = 10;
private var myTimer:Timer = new Timer(10);
private var secString = "00";
private var minString = Number(minutes);

这是我想要显示的关卡信息。

private var levels:Array = [
["01", "25", "50", "No Ante", "25", "50", "100", "500", "1000"],
["02", "50", "100", "No Ante", "25", "50", "100", "500", "1000"],
["03", "75", "150", "No Ante", "25", "50", "100", "500", "1000"],
["04", "100", "200", "No Ante", "25", "50", "100", "500", "1000"],
["05", "150", "300", "No Ante", "25", "50", "100", "500", "1000"],
["06", "200", "400", "No Ante", "25", "50", "100", "500", "1000"],
["07", "250", "500", "No Ante", "25", "50", "100", "500", "1000"],
["08", "300", "600", "No Ante", "100", "500", "1000", "5000", "10000"],
["09", "400", "800", "No Ante", "100", "500", "1000", "5000", "10000"],
["10", "500", "1000", "100", "100", "500", "1000", "5000", "10000"],
["11", "600", "1200", "200", "100", "500", "1000", "5000", "10000"],
["12", "700", "1400", "300", "100", "500", "1000", "5000", "10000"],
["13", "800", "1600", "400", "100", "500", "1000", "5000", "10000"],
["14", "900", "1800", "500", "500", "1000", "5000", "10000", "25000"],
["15", "1000", "2000", "1000", "500", "1000", "5000", "10000", "25000"],
["16", "3000", "6000", "2000", "500", "1000", "5000", "10000", "25000"],
["17", "5000", "10000", "5000", "500", "1000", "5000", "10000", "25000"]
];

这是我的水平或每个级别的圆形,计时器需要重置回到开始时间和倒计时

private var levelindex:int = 0;

这是我的计时器功能不能正常工作

private function countTimer(e:TimerEvent):void {
if (seconds > 60) {
} else {
    seconds--;
    // minus one minus every 59 sec
    if (seconds == 59) {
        minutes--;
    }
    secString = String(seconds);
    minString = String(minutes);

    // sec under 10 gets a 0 in front
    if (seconds < 10) {
        secString = String("0" + seconds);
    }

    // minutes under 10 gets a zero in front like 00:09
    if (minutes < 10) {
        minString = String("0" + minutes);
    }

    // reset seconds back to 60
    if (seconds == 0 && minutes >= 1) {
        seconds = 60;
        if (minutes == 0) {
            minuteStored = minutes;
            nextRound();

        }
    }

    if (seconds == 0 && minutes == 0) {
        if (levelindex == 17) {
            trace("Game Over")
            myTimer.stop();
        }
        trace("Round Over now reset and move to next round")
        minuteStored = minutes;
        nextRound()

    }

}

// this is to convert it to a string text
master.pages.timerMc.timerTextMc.goldTextMask.numberField.text = minString + ":" +    secString;
}

1 个答案:

答案 0 :(得分:0)

通常我会使用一个扩展Timer的CountdownTimer类。

可以通过指定总时间和间隔(以毫秒为单位)来实例化此计时器。

例如,以1秒的间隔从5分钟开始倒计时:

var countdownTimer:CountdownTimer = new CountdownTimer(5 * 60 * 1000, 1000);

在每个级别的开头,为所需的总时间实例化一个新的倒数计时器:

protected function startLevel():void
{
    countdownTimer = new CountdownTimer(5 * 1000 * 60);
    countdownTimer.addEventListener(TimerEvent.TIMER, timerHandler);
    countdownTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
    countdownTimer.start();
}

在每个计时器事件中,获取应用格式的剩余时间:

protected function timerHandler(event:TimerEvent):void
{
    var minutes:int = countdownTimer.time / 60000;
    var seconds:int = (countdownTimer.time / 1000) % 60;

    trace((minutes < 10 ? "0" + minutes.toString() : minutes.toString()) + ":" +
          (seconds < 10 ? "0" + seconds.toString() : seconds.toString()));
}

当计时器倒计时到零时,使用TimerEvent.TIMER_COMPLETE处理程序:

protected function timerCompleteHandler(event:TimerEvent):void
{
    trace("time's up!");
}

在关卡结束时,重置计时器:

protected function endLevel():void
{
    countdownTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
    countdownTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
    countdownTimer.reset();
}

CountdownTimer类实现:

package
{
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class CountdownTimer extends Timer
    {

        public var time:Number = 0;

        public function CountdownTimer(time:Number=Number.NEGATIVE_INFINITY, delay:Number=1000)
        {
            super(delay, repeatCount);

            if (isNaN(time))
                this.time = 0;
            else
                this.time = time;

            repeatCount = Math.ceil(time / delay);
        }

        override public function start():void
        {
            super.start();

            addEventListener(TimerEvent.TIMER, timerHandler);
            addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
        }

        protected function timerHandler(event:TimerEvent):void
        {
            time -= delay;
        }

        protected function timerCompleteHandler(event:TimerEvent):void
        {
        }

        override public function stop():void
        {
            super.stop();

            removeEventListener(TimerEvent.TIMER, timerHandler);
            removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
        }

    }
}