2012-03-06 25 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