A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: digital countdown

  1. #1
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94

    digital countdown

    Hello,
    I haved searched the forums for hours now trying to find a conclusive answer to this prob, as I am very short of time to get this sussed, but to no avail!
    I am trying to create a digital style clock that counts from 7.00 minutes down to zero, showing minutes, seconds and (possibly) milliseconds. Can anyone help me with this as time is running out!! (excuse the pun!!)

    Thanx in advance! :O)

  2. #2
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    You searched the forums for hours? Hmmm...

    Anyway, this will get you on your way:
    code:
    this.createTextField ("timer", 0, 100, 100, 250, 20);
    timer.text = "7 : 00";
    minutes = 7;
    seconds = 0;
    function countdown () {
    seconds--;
    if (seconds == -1) {
    minutes--;
    seconds = 59;
    }
    if (seconds < 10) {
    seconds = "0" + seconds;
    }
    if (minutes == 0 && seconds == 0) {
    clearInterval (timerInt);
    trace ("time up!");
    //do something
    }
    timer.text = minutes + " : " + seconds;
    }
    timerInt = setInterval (countdown, 1000);


  3. #3
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    Cheers for yr reply, but where would I put this code? inside an empty movieClip or just on the main timeline, or do I need to create a dynamic txt box? Sorry to be a pain :O)

  4. #4
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    Well the code I gave you creates its own textfield just for the purpose of demonstration, but you can create your own dynamic textfield, but make sure you give it an INSTANCE name of 'timer'. The code can go anywhere you want it too, and as long as the textfield's referenced properly it'll work. Try pasting it 'as-is' into the root of a blank movie and you'll pick up how it works.

    Have fun!

  5. #5
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    OK, i've got it to display the txt box, but how do i get it to actually start counting?!! sorry to bug you, but I really haven't got the time to try and figure it out, otherwise I would!!!

    Thanx for your time and help :O)

  6. #6
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    It should work straight away from the code above. The line:
    code:
    timerInt = setInterval (countdown, 1000);


    Is the one that starts the counting - make sure you copied it with the rest of the code!

    This code is for MX by the way, it won't work with 5...

  7. #7
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    I have copied the code exactly as you posted it, including the "timerInt", but the countdown does not start. is there a way of triggering it with a button? perhaps this is easier for me to get my head around!!!
    Once again, thanx for yr time, and sorry for being a pain!

  8. #8
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    by the way, I am using MX as well :O)

  9. #9
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    AHA!!! finally sussed it! for some reason, when you copy & paste code from here into Flash it adds in spaces where they aren't needed so after a bit of fiddling (and some fine delaying tactics!) it all works fine!! Thanx for yr help! :O)

  10. #10
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    Glad you got it working

  11. #11
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    Just a quick question for mngn9drb, if yr still around! now I have my countdown working, how would I go about putting a stop and a reset button on it?
    Thanx :O)

  12. #12
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    Just pick this apart - I'm not sure of how you've set up the buttons, but this code contains all the eventualities you may come across. If you're applying code directly to the buttons, just cut and paste the parts between the function declaration.
    code:
    stopped = false;
    //code on the stop button
    stopButton.onRelease = function () {
    //stop interval we use to countdown
    clearInterval (timerInt);
    stopped = true;
    };
    //reset button
    resetButton.onRelease = function () {
    //reset back to defaults
    timer.text = "7 : 00";
    minutes = 7;
    seconds = 0;
    };
    //and some code to restart the timer again, if you want to!
    startButton.onRelease = function () {
    //make sure timer has actually stopped
    if (!stopped) {
    //it hasn't? let's not go any further
    break;
    } else {
    //otherwise, lets restart the timer
    timerInt = setInterval (countdown, 1000);
    //set flag to flase for further presses
    stopped = false;
    }
    };

    Check the comments to see what stuff does.

  13. #13
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    Wow! cheers for yr speedy reply! I'll check it out now! :O)

  14. #14
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    works a treat! Cheers for all yr help!

  15. #15
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    No worries. Glad to be of service!

  16. #16
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    Hey there mngn9drb! Its me again! Got another quick question for ya! Is there any way that during the countdown at 6 minutes I can tell a movie clip to play (one that sez 1 minute to go)? I really appreciate all the help yr giving me, thanx a lot!! :O)

  17. #17
    immoralator
    Join Date
    Dec 2001
    Location
    UK
    Posts
    270
    You just need to replicate some of the code to add an if statement in there to see if minutes == 1 and seconds == 0, play the countdown. Pretty simple. It's already checking the time anyway, so see what you can come up with!

  18. #18
    Richard Friscuolo
    Join Date
    Mar 2001
    Posts
    94
    Yep, sussed it after a bit of playin about. Just wanted to say cheers for all yr help again! much appreciated.
    :O)

  19. #19
    Senior Member
    Join Date
    Apr 2003
    Location
    Never Never Land
    Posts
    120
    Hi ya all!

    I'm creating a clock that I can set to start at what time I choose but when seconds go to 60, it starts to add zeros into the minutes.

    Could someone point out whats wrong with my code.

    Code:
    this.createTextField ("timer", 0, 100, 100, 250, 20);
    timer.text = "14 : 59 : 50";
    hours = 14;
    minutes = 59;
    seconds = 50;
    function countdown () {
            seconds++;
            if (seconds == 60) {
                    minutes++;
                    seconds = 00;
            }
    	if (minutes == 60) {
    		hours++;
    		minutes = 00;
    	}
            if (seconds < 10) {
                    seconds = "0" + seconds;
    		}
    	if (minutes < 10) {
    		minutes = "0" + minutes;
    	}
            timer.text = hours + " : " + minutes + " : " + seconds;
    }
    timerInt = setInterval (countdown, 1000);
    Last edited by ramzez; 07-11-2003 at 08:38 AM.

  20. #20
    Senior Member
    Join Date
    Apr 2003
    Location
    Never Never Land
    Posts
    120
    Nevermind, found it myself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center