BoneStarr
BoneStarr

Reputation: 195

Adding time to a timer/counter

I've looked all over the web and everyone can teach you how to make a timer for your game or a countdown, but I can't seem to find out how to add time to an already counting timer.

So here is my counter class:

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;

        public function Score(stageRef:Stage)
        {
            x = 560.95;
            y = 31.35;
            this.stageRef = stageRef;

            timer.addEventListener(TimerEvent.TIMER, scoreTimer);
            timer.start();
        }

        public function scoreTimer(evt:TimerEvent):void
        {
            second += 1;
            scoreDisplay.text = String("Score: " +second);
        }

That works without any issues or problems and just keeps counting upwards at a speed of 100ms, what I want to know is how to add say 30 seconds if something happens in my game, say you kill an enemy for example.

Please help!

Upvotes: 0

Views: 1103

Answers (2)

Humble Hedgehog
Humble Hedgehog

Reputation: 2065

This can be as simple as calling a method which updates your second property. Your methods would end up looking something like this.

public function scoreTimer(evt:TimerEvent):void
{

    second += 1;
    updateDisplay();
}

public function addPenalty(penalty:Number):void
{

    second += penalty;
    updateDisplay();
}

private function updateDisplay()  : void
{

    scoreDisplay.text = String("Score: " +second);
}

Because you don't use your timer's values for updating the display, a reset or delay won't adjust your scoreDisplay. Instead it will just keep on counting where it left off because the second property itself is not reset during these actions.

Upvotes: 2

evilpenguin
evilpenguin

Reputation: 5478

I think Timer.delay can be used for that, something like Timer.delay = time_remaining + 30000 to add 30 secs to the timer. Sure, that implies you must know the time_remaining

If you set the delay interval while the timer is running, the timer will restart at the same repeatCount iteration.

quoted from here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html#delay

Upvotes: 0

Related Questions