当游戏耗尽时需要计时器重置游戏

时间:2014-08-10 23:22:56

标签: timer actionscript

我需要计时器才能在游戏结束时重置。定时器设置为1:05以从该值开始倒计时,但定时器在它用完时应该重置,它不会只停留在0:00。以下是代码:

package  {

    import flash.display.MovieClip;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import flash.events.ThrottleEvent;

    public class MainTimer extends MovieClip {

        private var currentMin:int;
        private var currentSec:int;

        private var oneSecondTimer:Timer = new Timer(1000,1);
        public var timerHasStopped:Boolean = false;

        public function MainTimer() {
            // constructor code
            trace("the main timer is here");
            currentMin = 1; 
            currentSec = 5; 

            minBox.text = String(currentMin);
            if(currentSec < 10) {
                secBox.text = "0" + String(currentSec);
            }else{
            secBox.text = String(currentSec);
        }
        oneSecondTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
        oneSecondTimer.start();
        }

        private function onTimerComplete(event:TimerEvent):void{
            currentSec = currentSec - 1;
            if (currentSec < 0){
                currentSec = 59;
                currentMin = currentMin - 1;
            }
            if(currentMin < 0) {
                currentMin = 0;
                currentSec = 0;
            }else{
                oneSecondTimer.start();
            }

            minBox.text = String(currentMin);
            secBox.text = String(currentSec);
            if(currentSec < 10){
                secBox.text = "0" + String(currentSec);
            }
        }

        public function resetTimer():void{
            //update our display
            currentMin = 1;
            currentSec = 5;
            minBox.text = String(currentMin);
            secBox.text = String(currentSec);
            //Adjust display for seconds less than 10
            if (currentSec < 10){
                secBox.text = "0" + String(currentSec);
            }//end if
            timerHasStopped = false;
            oneSecondTimer.start();
        }//end function
    }   
}

1 个答案:

答案 0 :(得分:0)

我修改了你的代码。您的代码未调用resetTimer函数。另外,我听了TIMER而不是TIMER_COMPLETE。当计时器达到0时,它调用resetTimer

package  {

import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
//import flash.events.ThrottleEvent;

public class MainTimer extends MovieClip {

    private var currentMin:int;
    private var currentSec:int;

    private var oneSecondTimer:Timer = new Timer(1000);
    public var timerHasStopped:Boolean = false;

    public function MainTimer() {
        // constructor code
        trace("the main timer is here");
        currentMin = 1; 
        currentSec = 5; 

        minBox.text = String(currentMin);
        if(currentSec < 10) {
            secBox.text = "0" + String(currentSec);
        }else{
        secBox.text = String(currentSec);
    }
    oneSecondTimer.addEventListener(TimerEvent.TIMER, onTimerComplete);     
    oneSecondTimer.start();
    }

    private function onTimerComplete(event:TimerEvent):void{

        trace("TIMER HAS STARTED. COUNTING DOWN.......");           

        currentSec = currentSec - 1;
        if (currentSec < 0){
            currentSec = 59;
            currentMin = currentMin - 1;
        }
        if(currentMin < 0) {
            currentMin = 0;
            currentSec = 0;
            resetTimer();
        }

        minBox.text = String(currentMin);
        secBox.text = String(currentSec);
        if(currentSec < 10){
            secBox.text = "0" + String(currentSec);
        }
    }

    public function resetTimer():void{
        oneSecondTimer.removeEventListener(TimerEvent.TIMER, onTimerComplete);
        trace("TIMER HAS FINISHED. RESETTING.......");
        //update our display
        currentMin = 1;
        currentSec = 5;
        minBox.text = String(currentMin);
        secBox.text = String(currentSec);
        //Adjust display for seconds less than 10
        if (currentSec < 10){
            secBox.text = "0" + String(currentSec);
        }//end if
        timerHasStopped = false;
        //if the timer needs to start again, add the following line of code.
        oneSecondTimer.addEventListener(TimerEvent.TIMER, onTimerComplete);

    }//end function
}   

}

希望这有帮助。