A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Timer problem!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    15

    Timer problem!

    I'm trying to set up a timer with custom graphics (MovieClips) and I've basically got it to work, but it's acting really strange! Maybe someone here can tell me what's wrong.

    I have two MC's on stage, named "secs_0" & "secs_1".
    They're both instances of another MC ("numbers"), which contain 10 frames with one digit on every frame (0 - 9).

    The weird thing is that the counter starts from 87! When the actual value of maxTime is 97...
    Also, when it reaches 80, 70 etc... it pauses and starts again at 78, 68...

    Here's the code

    Actionscript Code:
    var maxTime:Number = 99;
    var gameTimer:Timer = new Timer(1000);
    gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
    gameTimer.start();

    secs_0.stop();
    secs_1.stop();

    function updateTime(e:TimerEvent):void {
       
        maxTime --;
       
        var timeString:String = String(maxTime);
        secs_0.gotoAndStop(timeString.charAt(0) );
        secs_1.gotoAndStop(timeString.charAt(1) );
        trace(maxTime);
    }


    Any help is appreciated!

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    secs_0.stop();
    secs_1.stop();
    
    var maxTime:Number = 99;
    var gameTimer:Timer = new Timer(1000);
    gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
    gameTimer.start();
    updateTime();
    
    function updateTime(e:TimerEvent=null):void {
    	var timeString:String = String(maxTime);
    	if (maxTime > 9) {
    		secs_0.gotoAndStop(Number(timeString.charAt(0))+1 );
    		secs_1.gotoAndStop(Number(timeString.charAt(1))+1 );
    	} else {
    		secs_0.gotoAndStop(1);
    		secs_1.gotoAndStop(Number(timeString.charAt(0))+1 );
    	}
    	if (--maxTime < 0) {
    		gameTimer.stop();
    	}
    }

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    15
    Thank you so much, that worked perfectly!

    I do have one question though...

    Actionscript Code:
    if (--maxTime < 0) {

    I don't know if this is a weird question to ask but, how or why does Flash know it's supposed to do the -- in the last parenthesis? I've only studies AS3 for 4,5 months or so, and I didn't know you could have that kind of statements in there!

    Thanks for the help and for the neat code!

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    In this case, when the compiler sees the two minus signs before a number, it knows to decrement the number before doing the comparision.

    So this is just a shorthand way of writing
    Code:
    maxTime = maxTime - 1;
    if (maxTime < 0) {
    
    }

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    15
    Ahhh, very nice. I'll keep this in mind in the future!

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