1

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); 
    }
Nathan
  • 536
  • 4
  • 21

1 Answers1

1

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);
Bennett Keller
  • 1,724
  • 2
  • 11
  • 11
  • Thanks that seemed to do it. But now there is a another bug. It seems as if the problem I have is the same but now backwards. The timer now counts down to a negative number i checked and its counting down to the negatives from all the time that was added during the hittest. – Nathan Mar 06 '14 at 19:59
  • Ok so you are going to have to adjust your variables that are displaying the count etc. I can add that into my answer if needed, but that part is fairly easy to resolve. – Bennett Keller Mar 06 '14 at 20:04
  • Ill try to resolve it myself. But if I can't get it to work ill ask for help if thats okay. Thanks a lot man! – Nathan Mar 06 '14 at 20:16
  • I added it to my answer just in case. – Bennett Keller Mar 06 '14 at 20:34
  • Thank you good sir! Everything seems to be working perfect now! – Nathan Mar 07 '14 at 05:27
  • Wait no after further review I still have a bug haha. When seconds are added to the timer it does go up but after a few tries say when the timer goes down to 5 and i do a hittest and add more seconds instead of going up +2 seconds it goes up say like plus 6 so it is incrementing for some reason. Should i post the code i have now? – Nathan Mar 07 '14 at 10:46
  • In my current code changes there isn't anything that would do that. It sounds like you have a new issue now where your hit test is running more than once, leading to what may seem like a bug with the time increments. Just to be certain, I ran this locally increasing the time amount on a mouse click and everything works as expected. So you'll probably have to do some digging into how your hit test is running (verify it's perhaps running more than just once per collision). – Bennett Keller Mar 07 '14 at 14:27
  • Alright so i checked whats going on i traced everything and nothing is initiating twice. So lets say the counter is at 2 and i click and add 2 seconds now the counter is at 4 so lets say it goes down to 2 again and i click to add more time now the counter is at 6 instead of 4 so everytime i add 2 it keeps the original count and just adds to it. Maybe i am doing something wrong. Ill post above my updated code if you can still help me ! – Nathan Mar 08 '14 at 01:10
  • Ah of course, remember when I said the elapsedTime could be removed? I see the issue now, elapsedTime is increasing and you are adding that to the count, so basically first call elapsedTime += 2, next elapsedTime += 2 so it is at a value of 4...next += 2 elapsed = 6, etc. Just remove the elapsedTime variable and increment by what you want. Or set a variable to increment by and do that each time. I'll change it in my answer so you can reference that, I apologize I wasn't using that in my test version so I hadn't noticed. – Bennett Keller Mar 08 '14 at 02:35