Search code examples
actionscript-3flashactionscriptflash-cs6

5 Minute Countdown With Buttons


I have to make a 5 minute countdown clock as an assignment. I was given the basis of a as3 and told to add 'Start' - 'Reset' - 'Stop' buttons. So far I have a respectable countdown going, but no way to control it. I'm new to as2 and flash so I hope this isn't one of those "If you know that, you should know your answer" situations. Anything i try to find on the web as far as tuts don't really help me so if anyone could just tell me if I'm on the right path or eve if it's possible to ass buttons to this code to do whats stated above. Thanks in adv :)

var secs = "0" + 0;
    var mins = 5;
    timerDisplay.text = mins + ":" + secs;

    var timerInterval = setInterval(countDown,1000);
    //DISPLAYS DYNAMIC TEXT
    function countDown()
    {
        secs--;
        if (secs < 0)
        {
            secs = 59;
            mins--;
        }
        if (secs < 10)
        {
            var secs2 = "0" + secs;
        }
        else
        {
            var secs2 = secs;
        }

        if (mins == 0 && secs == 0)
        {
            clearInterval(timerInterval);//STOPS TIME @ ZERO
        }
        timerDisplay.text = mins + ":" + secs2;
    }

Solution

  • As this is an assignment, I won't give you any code, just some ideas to get you on your way to writing your own.

    You may want to add two (possibly three) buttons to the stage. One button could be the reset button and another could be a button that trades off between being start and stop (depending on whether the timer is currently running).

    The only code necessary for a reset button, is something that sets your min variable and your secs variable back to their original values (possibly plus 1 second because of your interval code). If you need help getting started with buttons clicks throwing functions check out the mc.onPress method.

    The only code necessary for a stop button, is something that stops your interval counter from continuing to count. I believe you already have something doing that when "clear" your timer at zero.

    The only code necessary for a start button, is something that restarts your interval counter. You do something of the sort when you first start your timerInterval.

    This won't work if someone decides to click on the start button after you have finished the countdown or if someone decides to click the start button multiple times.

    In the first case, the countdown will continue into negative numbers, so you may want to write an if statement that doesn't allow that to happen (inside of the start button function).

    And in the second case, the countdown will get faster and faster each time the button is pressed. Creating a boolean that keeps track of whether the program is stopped could possibly help with that problem.

    To clarify a statement I made above about the interval code: Your code will forever decrement every 1000 milliseconds. The text box is only ever being refreshed when you decrement, so if you try to reset with exactly 5 minutes and zero seconds, you will see the numbers jump to 4:59 and then keep decrementing. If you reset to 5 minutes and 1 second, the numbers will appear to jump to 5:00 and then decrement from there.

    I hope this helps!