嘿大家所以我有一個倒計時器,我已經設法實現在我的遊戲中,我在一些論壇上看到。現在,我設置了一個新功能,當用戶使用電影剪輯進行測試時,它會爲計時器添加一些秒數,但它似乎無法正常工作。我得到的錯誤是增加了更多時間,而不是真正添加它,因爲當我確實增加了更多的時間,並且說定時器倒數到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);
}
謝謝,似乎這樣做。但現在又出現了另一個bug。似乎我的問題是一樣的,但現在倒退了。計時器現在倒計數到我檢查的一個負數,並且它在倒數時間內一直添加到負數。 – Nathan
好,所以你將不得不調整顯示計數等的變量。如果需要,我可以將其添加到我的答案中,但該部分相當容易解決。 –
我試着自己解決它。但是,如果我無法讓它工作,生病可以請求幫助。非常感謝! – Nathan