Skip to main content
Known Participant
March 11, 2019
Question

trying to make reset button to reset timer back to 05:00

  • March 11, 2019
  • 2 replies
  • 805 views

start and stop works but cant find a way to get restart button to work

package  {

import flash.display.MovieClip;

import flash.utils.Timer;

import flash.events.TimerEvent;

import flash.events.MouseEvent;

import flash.ui.Mouse;

public class timerClass extends MovieClip {

  var myTimer:Timer = new Timer(1000, 300);

  var i:Number = 300;

public function timerClass() {

   // constructor code

   timerTxt.text = String("05:00");

   myTimer.addEventListener(TimerEvent.TIMER, updateTime);

   myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete);

   startbutton.addEventListener(MouseEvent.CLICK, StartNow);

   pausebutton.addEventListener(MouseEvent.CLICK, PauseNow);

   restartbutton.addEventListener(MouseEvent.CLICK, restartNow);

   }

  private function updateTime(e:TimerEvent):void{

   i--;

   var totalSeconds:* = i;

   var minutes:* = Math.floor(totalSeconds/60);

   var seconds:* = totalSeconds % 60;

   if(String(minutes).length < 2)

    minutes = "0" + minutes;

    if(String(seconds).length < 2)

    seconds = "0" + seconds;

   timerTxt.text = minutes + ":" + seconds;

  }

 

  private function TimerComplete(e:TimerEvent):void{

   messageTxt.text = "PRESENTATION IS NOW OVER"

   timerTxt.text = String("00:00");

  }

  private function StartNow(e:MouseEvent):void{

   myTimer.start();

  }

  private function PauseNow(e:MouseEvent):void{

   myTimer.stop();

 

  }

   private function restartNow(e:MouseEvent):void{

   }

  }

}

This topic has been closed for replies.

2 replies

Known Participant
March 20, 2019

I have a countdown timer I am trying to use in a game like second life its works fine in world the trouble is that only the one that presses start button can see the timer countdown

any sugestions

package  {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.ui.Mouse;

public class timerClass extends MovieClip {
  var myTimer:Timer = new Timer(1000, 300);
  var i:Number = 300;
public function timerClass() {
   // constructor code
   timerTxt.text = String("05:00");
   myTimer.addEventListener(TimerEvent.TIMER, updateTime);
   myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete);
   startbutton.addEventListener(MouseEvent.CLICK, StartNow);
   pausebutton.addEventListener(MouseEvent.CLICK, PauseNow);
   restartbutton.addEventListener(MouseEvent.CLICK, restartNow);
   }
  private function updateTime(e:TimerEvent){
   i--;
   var totalSeconds:* = i;
   var minutes:* = Math.floor(totalSeconds/60);
   var seconds:* = totalSeconds % 60;
   if(String(minutes).length < 2)
    minutes = "0" + minutes;
    if(String(seconds).length < 2)
    seconds = "0" + seconds;
   timerTxt.text = minutes + ":" + seconds;
  }
 
  private function TimerComplete(e:TimerEvent){
   messageTxt.text = "PRESENTATION IS NOW OVER"
   timerTxt.text = String("00:00");
  }
  private function StartNow(e:MouseEvent){
   myTimer.start();
 
  }
 
  private function PauseNow(e:MouseEvent){
   myTimer.stop();
  }
 
  private function restartNow(e: MouseEvent): void{ 
    myTimer.stop(); 
    myTimer = new Timer(1000, 300); 
    myTimer.addEventListener(TimerEvent.TIMER, updateTime); 
    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete); 
    i = 300; 
    messageTxt.text = "" 
    timerTxt.text = String("05:00"); 
        
  }
}
}

JoãoCésar17023019
Community Expert
Community Expert
March 11, 2019

Hi.

You need to stop the timer, reset the i variable, reset your text field to the total time, and then start the timer again. For this, your restartNow function could look like this:

private function restartNow(e:MouseEvent): void

{

    myTimer.stop();

    i = 300;

    timerTxt.text = String("05:00");

    myTimer.start();          

}

Regards,

JC

Known Participant
March 11, 2019

ok that works great unti the end when it runs down to 00:00 and displays the message txt then it wont restart again

any suggestions

thank you

package  {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.ui.Mouse;

public class timerClass extends MovieClip {
  var myTimer:Timer = new Timer(1000, 300);
  var i:Number = 300;
public function timerClass() {
   // constructor code
   timerTxt.text = String("05:00");
   myTimer.addEventListener(TimerEvent.TIMER, updateTime);
   myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete);
   startbutton.addEventListener(MouseEvent.CLICK, StartNow);
   pausebutton.addEventListener(MouseEvent.CLICK, PauseNow);
   restartbutton.addEventListener(MouseEvent.CLICK, restartNow);
   }
  private function updateTime(e:TimerEvent){
   i--;
   var totalSeconds:* = i;
   var minutes:* = Math.floor(totalSeconds/60);
   var seconds:* = totalSeconds % 60;
   if(String(minutes).length < 2)
    minutes = "0" + minutes;
    if(String(seconds).length < 2)
    seconds = "0" + seconds;
   timerTxt.text = minutes + ":" + seconds;
  }
 
  private function TimerComplete(e:TimerEvent){
   messageTxt.text = "PRESENTATION IS NOW OVER"
   timerTxt.text = String("00:00");
  }
  private function StartNow(e:MouseEvent){
   myTimer.start();
 
  }
 
  private function PauseNow(e:MouseEvent){
   myTimer.stop();
  }
 
  private function restartNow(e:MouseEvent): void{ 
        myTimer.stop(); 
        i = 300; 
        timerTxt.text = String("05:00"); 
        myTimer.start();      
  }
}
}

JoãoCésar17023019
Community Expert
Community Expert
March 11, 2019

OK.

Try this:

private function restartNow(e: MouseEvent): void

{

    myTimer.stop();

    myTimer = new Timer(1000, 7);

    myTimer.addEventListener(TimerEvent.TIMER, updateTime);

    myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete);

    i = 7;

    messageTxt.text = ""

    timerTxt.text = String("05:00");

    myTimer.start();           

}

As the code is repeating too much it will be a good idea to wrap some code inside of functions and store some hardcoded values in variables.

Regards,

JC