Question

Hey everyone So I have a countdown timer that I have managed to implement in my game that I saw on some forums. Now I set up a new function to add some seconds to the timer whenever the user Hittest with a Movie Clip but it doesnt seem to work right the errors I'm getting is while it does add more time, It's not really adding it because when i do add more time and say the timer counts down to 10 depending on how much more seconds I added with the Hittest the game ends on 10 rather than 0 seconds. So I don't think its actually changing the time interval internally.

Also when I restart the game it shows the past time for the one second interval then once it starts counting down it finally resets and starts counting down normally.

Here is how I have it setup:

My initialized variables in my constructor:

count = 30; //Count down from 60
        myTimer = new Timer(1000, count);// Timer intervall in ms

        myTimer.addEventListener(TimerEvent.TIMER, countdown);//EventListener for intervalls
        myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete);
        myTimer.start();//start Timer

The countDown function:

private function countdown(e:TimerEvent):void 
    {
         //Display Time in a textfield on stage

        countDownTextField.text = String((count)- myTimer.currentCount); 

         updateCountdownTimer();

    }

In my HitTest Function:

private function onPopIsClicked(pop:DisplayObject):void 
    {

                nScore ++;
                updateHighScore();
                updateCurrentScore();

                //Add Explosion Effect
                popExplode = new mcPopExplode();
                stage.addChild(popExplode);
                popExplode.y = mPop.y;
                popExplode.x = mPop.x;

                elapsedTime += 5;
                updateCountdownTimer();

    }

Then finally in my updateCountdownTimer():

public function updateCountdownTimer():void
    {
        countDownTextField.text = String((count)- myTimer.currentCount + elapsedTime); 
    }

Can you see why I can't add more seconds to the timer correctly?

Thanks in advance.

**** UPDATE ******

count = 10; //Count down from 10
        myTimer = new Timer(1000, count);// Timer intervall in ms
        updateCountdownTimer();

        myTimer.addEventListener(TimerEvent.TIMER, countdown);//EventListener for intervalls
        myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete);
        myTimer.start();//start Timer

private function countdown(e:TimerEvent):void 
    {
         //Display Time in a textfield on stage
         updateCountdownTimer();

    }

private function onPopIsClicked(pop:DisplayObject):void 
    {


                elapsedTime += 2;
                myTimer.repeatCount += elapsedTime; //add time to the timer
                count += elapsedTime;
                updateCountdownTimer();
                trace("pop clicked and time");

    }

public function updateCountdownTimer():void
    {
        countDownTextField.text = String((count)- myTimer.currentCount); 
    }
Was it helpful?

Solution

So the solution to add time to the Timer is to adjust the repeatCount property. You would do that in your onPopIsClicked():

private function onPopIsClicked(pop:DisplayObject):void 
{

     nScore ++;
     updateHighScore();
     updateCurrentScore();

     //Add Explosion Effect
     popExplode = new mcPopExplode();
     stage.addChild(popExplode);
     popExplode.y = mPop.y;
     popExplode.x = mPop.x;

     elapsedTime += 5;
     myTimer.repeatCount += 5; //add time to the timer
     updateCountdownTimer();

}

So you will need to change how you display your countdown since we are increasing the repeatCount property. What will happen is your myTimer.currentCount may increase past the size of your count variable. So in your onPopIsClicked() add this line after you increment elapsedTime:

elapsedTime += 5;
myTimer.repeatCount += 5;
count += 5//this line, increment the count total

You can also set a variable to increment by if you wanted:

var increaseBy:int = 5

Then you would use that with repeatCount, count, and elapsedTime (if you need that still):

elapsedTime += increaseBy;
myTimer.repeatCount += increaseBy;
count += increaseBy;

You really don't need elapsedTime anymore unless you are using it for something else. Then you update your text, there is no need to add the elapsedTime so it would look like:

countDownTextField.text = String(count- myTimer.currentCount);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top