Timer won't reset

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);
}

Answers


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 seconds variable.

public function resetTimer(): void 
{
    seconds = 0;
    timer.reset();
    timer.start();
    updateDisplay();
}

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 seconds variable. So manipulating the Timer won't change the seconds nor the display.


Need Your Help

how to make selenium and chromium work on ubuntu

python selenium selenium-webdriver chromium selenium-chromedriver

I am trying to use selenium with Chrome to browse a website that uses javascript.

Making a smart listeners notifier to avoid code duplication everywhere

java algorithm data-structures collections arraylist

ATTENTION: The code is thread-safe, in other words, single-threaded. Only I can remove myself from the list of listeners.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.