0
votes

I need the timer to be able to reset when the game ends. The Timer is set to 1:05 to count down from that value but the timer when it is supposed to reset when it runs out, it does not it just stays at 0:00. Here is the code below:

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

1 Answers

0
votes

I have amended your code. Your code wasn't calling the resetTimer function. Also, I've listen for TIMER instead of TIMER_COMPLETE. When the timer reaches 0, its calls the 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
}   

}

Hope this helps.