向定时器添加和减去时间的问题

时间:2015-01-09 02:56:14

标签: actionscript-3

现在我也在第8帧使用计时器,这是我的Gamescreen框架,试图创建一个能量条,每秒减少1次,每次角色与对象碰撞,然后将计数值增加1 (在我的最小值是1秒,对吗?),但是计时器过早耗尽,当标签显示3秒后收集3个项目时计时器自动结束,请帮助我! :)

var count:Number = 5; (temporary value for testing)
var theTimer:Timer = new Timer(1000, count);
theTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
theTimer.start();

function whenTimerComplete(e:TimerEvent):void
{
    theTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, whenTimerComplete); //Remove listener
    gotoAndStop("frameFive"); // Advance to score screen.
}

theTimer.addEventListener(TimerEvent.TIMER, theCountdown);

function theCountdown(e:TimerEvent):void
{
    count--;
    timerLabel.text = count.toString()
}

//Start the timer and show in the label.
timerLabel.text=count.toString();
theTimer.start();

非常感谢所有帮助和解决方案。

1 个答案:

答案 0 :(得分:0)

这是一个倒计时器示例:

enter image description here

在FLA的根目录创建一个倒计时器类:

<强> CountdownTimer.as

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 = time;

        repeatCount = Math.ceil(time / delay);

        addEventListener(TimerEvent.TIMER, timerHandler);
    }

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

        if (time == 0)
            dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
    }

    public function dispose():void {
        removeEventListener(TimerEvent.TIMER, timerHandler);
    }

}
}

在FLA的时间轴上,创建一个计时器,其中包含倒计时的总毫秒数:

var timer:CountdownTimer = new CountdownTimer(60000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();

在上面的示例中,计时器将倒计时1分钟(60秒)。每隔一秒timerHandler将被调用。当它达到0时,将调用timerCompleteHandler

function timerHandler(event:TimerEvent):void {
    timerText.text = (timer.time / 1000).toString();
}

function timerCompleteHandler(event:TimerEvent):void {
    timerText.text = "COMPLETE";
}

要为计时器添加时间,请将毫秒添加到time。如果您希望计时器在达到0时调度计时器完成事件,请更新repeatCount

timer.time += 1000;
timer.repeatCount += 1;

同样从计时器中删除时间,从time减去毫秒;再次,如果您希望计时器在达到0时调度计时器完成事件,请更新repeatCount

timer.time -= 1000;
timer.repeatCount -= 1;