如何在计时器运行时减少计时器的时间(ActionScript 3.0)

时间:2012-03-06 05:03:59

标签: actionscript-3 flash timer

在我的情况下,每当调用函数时,我制作的计时器都不会缩短它的时间。我会更改或添加哪些代码以减少计时器的时间?

计时器代码:

var count:Number = 1200;
var lessTime:Number = 180;
var totalSecondsLeft:Number = 0;
var timer:Timer = new Timer(1000, count);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timesup);

function countdown(event:TimerEvent) {
    totalSecondsLeft = count - timer.currentCount;
    this.mainmc.time_txt.text = timeFormat(totalSecondsLeft);
}

function timeFormat(seconds:int):String {
var minutes:int;
var sMinutes:String;
var sSeconds:String;
if(seconds > 59) {
    minutes = Math.floor(seconds / 60);
    sMinutes = String(minutes);
    sSeconds = String(seconds % 60);
    } else {
    sMinutes = "";
    sSeconds = String(seconds);
}
if(sSeconds.length == 1) {
    sSeconds = "0" + sSeconds;
}
return sMinutes + ":" + sSeconds;
}

function timesup(e:TimerEvent):void {
    gotoAndPlay(14);
}

此时timer.start();被放置在一个框架上,以便计时器在进入框架时开始。

1 个答案:

答案 0 :(得分:3)

delay上的Timer属性正是​​您要找的。在您的处理程序中,更改计时器的延迟:

function countdown(event:TimerEvent)
{
    totalSecondsLeft = count - timer.currentCount;
    this.mainmc.time_txt.text = timeFormat(totalSecondsLeft);

    //change the timer delay
    timer.delay -= lessTime;
}

我假设您的代码示例要在每个定时器间隔的定时器延迟中减去lessTime。如果您想将延迟更改为其他内容,请相应地调整代码。

<强>更新
上面的代码用于减少每个计时器触发之间的间隔(delay)。如果你想要做的是减少计时器到达repeatCount所需的间隔(TIMER_COMPLETE),那么你想要更改repeatCount属性{ {1}}:

Timer

另一个更新
请注意,当您更改//set the timer fire interval to 1 second (1000 milliseconds) //and the total timer time to 1200 seconds (1200 repeatCount) var timer:Timer = new Timer(1000, 1200); //reduce the overall timer length by 3 minutes timer.repeatCount -= 300; 时,它不会影响repeatCount。由于您使用单独的currentCount变量和count来计算剩余的显示时间,因此看起来没有任何变化。实际上它是 - 计时器将在显示的时间倒数到零之前完成。为了让您的时间显示准确无误,请务必从timer.currentCount中减去与count相同的金额:

repeatCount