2012-03-06 54 views
0

在我的情况下,计时器我做不会降低它的时间,每当一个函数被调用。我将更改或添加哪些代码以减少计时器中的时间?如何在定时器运行(ActionScript 3.0中)减少定时器的时间

定时器代码:

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();使得定时器开始,因为它进入所述框架被放置在框架上。

回答

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您的代码示例。如果您想将延迟更改为其他内容,请相应地调整代码。

UPDATE
上面的代码是用于降低每个定时器火之间的间隔(delay)。如果你想要做的反而是降低的时间间隔(repeatCount)的量需要定时器达到TIMER_COMPLETE,那么你想改变TimerrepeatCount属性:

//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来计算所显示的剩余时间,因此看起来不会发生任何变化。实际上它是 - 计时器在显示时间倒数到零之前完成。为了使您的剩余时间显示准确,确保你从repeatCountcount减去相同量:

timer.repeatCount -= 300; 
count -= 300; 
+0

当我运行该程序,添加代码,我的定时器下降速度快于正常秒计数。我试图做的是当一个监听器被激活时,计时器会减少3分钟。那可能吗? – 2012-03-06 06:19:34

+0

你是什么意思,“减少3分钟”?现在你的定时器间隔开始每秒触发(1000毫秒)。您不能将该时间间隔减少3分钟。你是在谈论定时器间隔(延迟)还是时间点击'TIMER_COMPLETE'? – redhotvengeance 2012-03-06 06:26:22

+0

我试图让连续10次点击后屏幕上的计时器减少3分钟。但我的计时器我不知道如何减少函数调用后的时间这里是函数的代码:this.missedclicks = 10; this.mainmc.addEventListener(MouseEvent.CLICK,clickOb1); // missclick 功能clickScreen(E:的MouseEvent){ \t this.missedclicks--; \t if(this.missedclicks == 0){ \t \t //改变定时器延迟 \t timer.delay - = lessTime; \t \t this.missedclicks = 5; \t \t} } – 2012-03-06 06:34:31