2014-03-06 41 views
1

嘿大家所以我有一個倒計時器,我已經設法實現在我的遊戲中,我在一些論壇上看到。現在,我設置了一個新功能,當用戶使用電影剪輯進行測試時,它會爲計時器添加一些秒數,但它似乎無法正常工作。我得到的錯誤是增加了更多時間,而不是真正添加它,因爲當我確實增加了更多的時間,並且說定時器倒數到10,這取決於我在Hittest遊戲以10秒而不是0秒結束時添加了多少秒。所以我不認爲它實際上在內部改變了時間間隔。如何在AS3中添加秒鐘到定時器

此外,當我重新啓動遊戲時,它會顯示一秒鐘間隔的過去時間,然後一旦它開始倒計時,最終重置並開始正常倒計時。

這是我如何把它設置:

我在我的構造函數初始化的變量:

count = 30; //Count down from 60 
     myTimer = new Timer(1000, count);// Timer intervall in ms 

     myTimer.addEventListener(TimerEvent.TIMER, countdown);//EventListener for intervalls 
     myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete); 
     myTimer.start();//start Timer 

倒計時功能:

private function countdown(e:TimerEvent):void 
    { 
     //Display Time in a textfield on stage 

     countDownTextField.text = String((count)- myTimer.currentCount); 

     updateCountdownTimer(); 

    } 

在我的HitTest功能:

private function onPopIsClicked(pop:DisplayObject):void 
    { 

       nScore ++; 
       updateHighScore(); 
       updateCurrentScore(); 

       //Add Explosion Effect 
       popExplode = new mcPopExplode(); 
       stage.addChild(popExplode); 
       popExplode.y = mPop.y; 
       popExplode.x = mPop.x; 

       elapsedTime += 5; 
       updateCountdownTimer(); 

    } 

然後終於在我的updateCountdownTimer():

public function updateCountdownTimer():void 
    { 
     countDownTextField.text = String((count)- myTimer.currentCount + elapsedTime); 
    } 

你可以看到爲什麼我不能正確秒鐘添加到計時器?

在此先感謝。

** * * UPDATE * ** * **

count = 10; //Count down from 10 
     myTimer = new Timer(1000, count);// Timer intervall in ms 
     updateCountdownTimer(); 

     myTimer.addEventListener(TimerEvent.TIMER, countdown);//EventListener for intervalls 
     myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete); 
     myTimer.start();//start Timer 

private function countdown(e:TimerEvent):void 
    { 
     //Display Time in a textfield on stage 
     updateCountdownTimer(); 

    } 

private function onPopIsClicked(pop:DisplayObject):void 
    { 


       elapsedTime += 2; 
       myTimer.repeatCount += elapsedTime; //add time to the timer 
       count += elapsedTime; 
       updateCountdownTimer(); 
       trace("pop clicked and time"); 

    } 

public function updateCountdownTimer():void 
    { 
     countDownTextField.text = String((count)- myTimer.currentCount); 
    } 

回答

1

因此該解決方案添加時間到Timer是調整repeatCount財產。你會做,在你的onPopIsClicked()

private function onPopIsClicked(pop:DisplayObject):void 
{ 

    nScore ++; 
    updateHighScore(); 
    updateCurrentScore(); 

    //Add Explosion Effect 
    popExplode = new mcPopExplode(); 
    stage.addChild(popExplode); 
    popExplode.y = mPop.y; 
    popExplode.x = mPop.x; 

    elapsedTime += 5; 
    myTimer.repeatCount += 5; //add time to the timer 
    updateCountdownTimer(); 

} 

所以,你需要改變你如何顯示您的倒計時,因爲我們增加了repeatCount財產。會發生什麼情況是您的myTimer.currentCount可能會增加超過您的count變量的大小。

elapsedTime += 5; 
myTimer.repeatCount += 5; 
count += 5//this line, increment the count total 

您還可以設置一個變量遞增的,如果你想:

var increaseBy:int = 5 

那麼你會使用與repeatCount,計數等你onPopIsClicked()你增加elapsedTime後添加此行elapsedTime(如果你需要的仍然):

elapsedTime += increaseBy; 
myTimer.repeatCount += increaseBy; 
count += increaseBy; 

你真的不,除非你正在使用它的事端需要elapsedTime了否則。然後你更新你的文本,沒有必要添加elapsedTime所以它看起來像這樣:

countDownTextField.text = String(count- myTimer.currentCount); 
+0

謝謝,似乎這樣做。但現在又出現了另一個bug。似乎我的問題是一樣的,但現在倒退了。計時器現在倒計數到我檢查的一個負數,並且它在倒數時間內一直添加到負數。 – Nathan

+0

好,所以你將不得不調整顯示計數等的變量。如果需要,我可以將其添加到我的答案中,但該部分相當容易解決。 –

+0

我試着自己解決它。但是,如果我無法讓它工作,生病可以請求幫助。非常感謝! – Nathan

相關問題