簡體   English   中英

如何減少計時器運行時的計時器時間(ActionScript 3.0)

[英]How to reduce the timer's time while timer is running (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(); 放置在框架上,以便計時器在進入框架時啟動。

您正在尋找Timerdelay屬性。 在您的處理程序中,更改計時器的延遲:

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

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

通過代碼示例,我假設您想從每個計時器間隔的計時器延遲中減去lessTime 如果要將延遲更改為其他值,則只需相應地調整代碼即可。

更新
上面的代碼用於減少每個計時器觸發之間的間隔( delay )。 如果您要做的是減少計時器達到TIMER_COMPLETE的間隔時間( repeatCount ),那么您想更改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來計算顯示的剩余時間,因此看起來沒有任何變化。 確實是這樣-計時器將在顯示的時間倒數到零之前完成。 為了使您的剩余時間顯示准確,請確保從count減去與從repeatCount減去相同的數量:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM