Extremely simple timer question
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Extremely simple timer question

  1. #1
    _root.username+"ns";
    Join Date
    Apr 2008
    Posts
    18

    Extremely simple timer question

    Hello, I haven't coded in literally 9 years so bear with me on the stupidity. I was used to AS2 as well so this is entirely new for me. I'm just teaching myself again and trying to make a simple counter that keeps track of minutes and seconds. However, when I trace, the seconds and minutes do not increase, and the "counter" variable just alternates between 1 and 2. What am I doing wrong?

    Code:
    //30 fps
    var counter:int = 0;
    var seconds:int = 0;
    var minutes:int = 0;
    var hours:int = 0;
    addEventListener(Event.ENTER_FRAME, time);
    function time(e:Event):void
    {
    	if (! pause)
    	{
    		if (counter<30)
    		{
    			counter++;
    		}
    		else
    		{
    			counter = 0;
    			if (seconds<59)
    			{
    				seconds++;
    			}
    			else
    			{
    				minutes++;
    				seconds = 0;
    			}
    		}
    	}
    	trace("Elapsed time: "+minutes+" minutes "+seconds+" seconds");
    }

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,151
    Hi,

    Heres one I found within this forum, it uses the current time and adds however much time you choose and counts down to that time,

    witht a textfield on the stage called time_txt
    PHP Code:
    var endTime:int getTimer();
    endTime +=  2.5 60 1000;//adjust endTime to however many minutes in the future.(2 and a half in this eample)
    // 4 * 60 * 1000 - 4 minutes in this example

    var countdownTimer:Timer = new Timer(1000);
    countdownTimer.addEventListener(TimerEvent.TIMERupdateTime);
    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;


  3. #3
    _root.username+"ns";
    Join Date
    Apr 2008
    Posts
    18
    I understand the use of the Timer class, but I really wanted to know why my code is just alternating the "counter" variable from 1 and 2, therefore causing the rest of the function to fail, mostly for learning purposes. Thank you either way.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,151
    Hi,

    When I test your code, I have the pause error, I comment it out and it traces this
    Code:
    Elapsed time: 0 minutes 0 seconds
    Elapsed time: 0 minutes 0 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 1 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 2 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 3 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 4 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 5 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds
    Elapsed time: 0 minutes 6 seconds

  5. #5
    _root.username+"ns";
    Join Date
    Apr 2008
    Posts
    18
    Nevermind, I am a complete idiot. I forgot I had another scene which it was looping back to and resetting all my variables again.

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

Poll by Flashkit.com