A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: countdown minute timer

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    9

    countdown minute timer

    Hi, sorry for my english but i'm italian. I'm trying to create a timer with as3 but it doesn't work. It will start from 15 minutes and it will arrive at zero, but it doensn't go!!! I'm not very able in as3 so anyone can tell me what's the problem?? The code


    var endDateate = new Date(2008,6,20,14,30,0);

    var countdownTimer:Timer = new Timer(1000);
    countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
    countdownTimer.start();

    function updateTime(e:TimerEvent):void
    {
    var nowate = new Date(2008,6,20,14,15,0);
    var timeLeft:Number = endDate.getTime() - now.getTime();
    var seconds:Number = Math.floor(timeLeft / 1000);
    var minutes:Number = Math.floor(seconds / 60);


    seconds %= 60;
    minutes %= 60;


    var sec:String = seconds.toString();
    var min:String = minutes.toString();

    if (sec.length < 2) {
    sec = "0" + sec;
    }

    if (min.length < 2) {
    min = "0" + min;
    }



    var time:String = min + ":" + sec;

    time_txt.text = time;
    }



    Thank you!!!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Since you are creating "now" with hardcoded values, the difference between "now" and "endDate" will never change. You should derive "now" from the current time (and you should probably derive "endDate" from the current time as well.
    Code:
    var endTime:int = getTimer();
    endTime += 15*60*1000;  //adjust endTime to 15 minutes in the future.
    
    var countdownTimer:Timer = new Timer(1000);
    countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
    countdownTimer.start();
    
    function updateTime(e:TimerEvent):void
    {
      var timeLeft:Number = endTime - getTimer();
      var seconds:Number = Math.floor(timeLeft / 1000);
      var minutes:Number = Math.floor(seconds / 60);
    
      seconds %= 60;
      minutes %= 60;
    
      var sec:String = seconds.toString();
      var min:String = minutes.toString();
      if (sec.length < 2) {
        sec = "0" + sec;
      }
    
      if (min.length < 2) {
        min = "0" + min;
      }
    
      var time:String = min + ":" + sec;
      time_txt.text = time;
    }
    Since you weren't actually using any of the fancy features of Date, I replaced it with getTimer.

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    Thank you!!!It works perfectly in a project in which i paste the code in the first frame... But if i insert the code in a project in which i use the object oriented programming and i don't insert the code in the frame but in the classes, it give me the errors below:

    1180: Call to a possibly undefined method getTimer.

    I also insert in the package the line:

    import flash.utils.Timer;

    Why does it give me that error??

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    Ok sorry!!!I have insert also the line

    import flash.utils.getTimer;

    and now it doesn't give me eny errors, but i don't see it on the display!
    I try to insert an addChild(time_txt); but nothing happens!Wyhy??Oh damn!

    Thank u very much for your answer!

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you have this in a separate class which you are using as a document class, you should change the time_txt line to something like this:

    Code:
    TextField(getChildByName("time_txt")).text = time;
    Or you could declare and instantiate time_txt all in code.

    If you are using a non-document class, the problem is probably something else.

  6. #6
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    Yes i have the code in a class, not in the Document Class. I use the line that you gave me, but in output i have this error:

    TypeError: Error #1009: Impossibile accedere a una proprietÃ* o a un metodo di un riferimento oggetto null.

    i translate: Impossible to access to a propriety or a refence method null object.
    Why??

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If this is a non-document class, then the problem is that it has no idea what time_txt is. You can create and add it within the class, or you can pass it to the class when you instantiate it. Here's the second way, which is a bit more flexible:
    Code:
    package{
    //imports here
    
    public class CountDown extends Sprite{
    
    private var endTime:int;
    private var time_txt:TextField;
    
    public function CountDown(tt:TextField):void{
      time_txt = tt;
      go();
    }
    
    
    public function go():void{
      endTime = getTimer();
      endTime += 15*60*1000;  //adjust endTime to 15 minutes in the future.
      var countdownTimer:Timer = new Timer(1000);
      countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
      countdownTimer.start();
    }
    
    function updateTime(e:TimerEvent):void
    {
      var timeLeft:Number = endTime - getTimer();
      var seconds:Number = Math.floor(timeLeft / 1000);
      var minutes:Number = Math.floor(seconds / 60);
    
      seconds %= 60;
      minutes %= 60;
    
      var sec:String = seconds.toString();
      var min:String = minutes.toString();
      if (sec.length < 2) {
        sec = "0" + sec;
      }
    
      if (min.length < 2) {
        min = "0" + min;
      }
    
      var time:String = min + ":" + sec;
      time_txt.text = time;
    }
    }//end class
    }//end package
    And you'd instantiate it like this in your document:
    Code:
    var countdown:CountDown = new CountDown(time_txt); //where time_txt is a TextField on the stage
    addChild(countdown);

  8. #8
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    Yes it doesn't give me any errors, but i don't see the timer on the display... I have put a trace after the addChild and i see on the output panel the written! Ohh i don't understeand why!I really thank u...

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Are you sure time_txt is on the stage?

    try this in the main document:
    Code:
    var time_txt2:TextField = new TextField();
    time_txt2.x = 100;
    time_txt2.y = 100;
    addChild(time_txt2);
    var countdown:CountDown = new CountDown(time_txt2);
    When it's implemented as above, CountDown does not itself have any visual appearance, so it does not need to extend Sprite. I've removed the addChild(countdown) line because it doesn't really do anything useful.

  10. #10
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    Yes it is a dynamic Text on the stage! i try with time_txt2 but another time i don't see the timer on the stage... Noooooo. I send you the directory with the project (onli the as files.)..if u want u can take a look at it.
    However rhank u!
    Attached Files Attached Files

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That's weird. I'm swamped at the moment, but I'll try to take a look at this tonight. Anyone else want to chime in?

  12. #12
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    What do u mean with " chime in"?Sorry but i dont' understeand!

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's just an expression. I was asking any of the other people on the board who may have read this thread whether they'd like to add anything to the discussion.

  14. #14
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    Ok i solved the problem!i didn't checked anti.alias for animation in the textField.

    I have another questions... if i want to reset the timer when i push on a botton what can i do?i have use timer.reset(); but the timer don't reset!

  15. #15
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To reset the timer, you'll have to keep an accessible reference to it around. Right now, it's declared as a local variable in the go function, which means you can't easily do anything with it later. You can move it up to the class level and add a reset like this:
    Code:
    public class CountDown {
    
    private var endTime:int;
    private var time_txt:TextField;
    private var countdownTimer:Timer;
    
    public function CountDown(tt:TextField):void{
      time_txt = tt;
      countdownTimer:Timer = new Timer(1000);
      countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
      go();
    }
    
    public function go():void{
      countdownTimer.reset();
      endTime = getTimer();
      endTime += 15*60*1000;  //adjust endTime to 15 minutes in the future.
      countdownTimer.start();
    }
    
    function updateTime(e:TimerEvent):void
    {
      var timeLeft:Number = endTime - getTimer();
      var seconds:Number = Math.floor(timeLeft / 1000);
      var minutes:Number = Math.floor(seconds / 60);
    
      seconds %= 60;
      minutes %= 60;
    
      var sec:String = seconds.toString();
      var min:String = minutes.toString();
      if (sec.length < 2) {
        sec = "0" + sec;
      }
    
      if (min.length < 2) {
        min = "0" + min;
      }
    
      var time:String = min + ":" + sec;
      time_txt.text = time;
    }
    
    /**
    * resets and restarts the internal timer.
    */
    public function reset():void{
      go();
    }
    }//end class
    I've re-arranged things again, so that go() can be used both for the initial start and to restart later. I've added a public restart method so that you can call countdown.restart() from outside the class, but you could just do it with go(). I've removed "extends Sprite" because it doesn't need to be a Sprite. Do not try to add a CountDown to the stage now.

  16. #16
    Junior Member
    Join Date
    Jun 2008
    Posts
    9
    I try with that code..and the timer restart but the problem is that the other timer remain in memory, so i have at the same time two timer...how can i free the memory??

  17. #17
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What other timer? Unless you created another with "new CoundDown()", there's only one.

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