Hey guys so I’ve got a working Timer, which will stop() when I call it to and start, but I can’t get it to reset(). It just starts from where it left off.
Code for the timer
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.text.TextField;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Score extends MovieClip
{
public var second:Number = 0;
public var timer:Timer = new Timer(100);
private var stageRef:Stage;
private var endScore:displayFinalScore;
public function Score(stageRef:Stage)
{
x = 560.95;
y = 31.35;
this.stageRef = stageRef;
this.endScore = endScore;
timer.addEventListener(TimerEvent.TIMER, scoreTimer);
timer.reset();
timer.start();
updateDisplay();
}
public function scoreTimer(evt:TimerEvent):void
{
second += 1;
updateDisplay();
}
public function get10Points(points:Number) : void
{
second += points;
updateDisplay();
}
public function finalScore() : void {
timer.stop();
}
public function resetTimer(): void {
timer.reset();
timer.start();
updateDisplay();
}
public function updateDisplay(): void
{
scoreDisplay.text = String("Score: " +second);
}
}
}
Code for where I call it to do things:
private function sendGameOver(e:Event) : void
{
//Stop the stage and remove everything
stop();
stage.removeChild(ourBoat);
stage.removeChild(score);
removeEventListener(Event.ENTER_FRAME, loop);
//Add the end score
addChild(endScore);
//Add the game over
addChild(endGame);
addChild(playAgain);
//Add the play again options
playAgain.addEventListener(MouseEvent.CLICK, newGame);
score.finalScore();
}
public function newGame (e:MouseEvent) :void
{
score.resetTimer();
gotoAndPlay(1);
removeChild(endGame);
removeChild(endScore);
removeChild(playAgain);
stage.addChild(ourBoat);
stage.addChild(score);
//Keep the boat within the desired boundaries
ourBoat.x = stage.stageWidth / 2;
ourBoat.y = stage.stageHeight - 100;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
In your
resetTimer()function you’re resetting your Timer (this works just fine). But the thing is you’re not using the Timer itself to calculate your seconds variable. So when you start the Timer again the seconds will continue where they left off.What you need to do is reset the
secondsvariable.Keep in mind that, in this case, your Timer is just a tool you use to display your own interpretation of the seconds stored in your
secondsvariable. So manipulating the Timer won’t change the seconds nor the display.