A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: TimerEvent only works once

  1. #1
    Member
    Join Date
    Dec 2004
    Posts
    68

    TimerEvent only works once

    I can only run a timer.start(); command once, the second time it will only run once instead of the value of 10 repeat.

    Code:
    var addTenPointsTimer:Timer=new Timer(500, 10);
    var currentScore = 0;
    score.text = "0";
    
    function addTenPoints(event:TimerEvent):void{
    	currentScore++
    	trace("add point");
    	score.text = currentScore;	
    }
    
    addTenPointsTimer.addEventListener(TimerEvent.TIMER, addTenPoints);
    
    function tenPointButton(e:MouseEvent):void{
    	addTenPointsTimer.start();
    }
    tenButton.addEventListener(MouseEvent.CLICK, tenPointButton);
    I want to be able to have the score add the ten points one at a time rather than all at once. It works fine the first time ten points are added, but the second, third, fourth, etc time it only adds one point then stops. How can I get the timer to work more than once?

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    That's because the timer will only run if it's count is less than 10, so the first time you call start it runs 10 times. Then when you call start () again it forces it to run one more time but since it's count is 11 it won't continue. You have to call addTenPointsTimer.reset (). This will reset the timer's count to 0. If you call this method while the timer is still running it will reset to 0 and continue running, if you call it when it's stopped, it will reset and stay stopped.

  3. #3
    Member
    Join Date
    Dec 2004
    Posts
    68
    Thanks, I was able to get it to work by adding a timer complete event with the reset:

    Code:
    var addTenPointsTimer:Timer=new Timer(500, 10);
    var currentScore = 0;
    score.text = "0";
    
    function addTenPoints(event:TimerEvent):void{
    	currentScore++
    	trace("add point");
    	score.text = currentScore;	
    }
    
    addTenPointsTimer.addEventListener(TimerEvent.TIMER, addTenPoints);
    
    function tenPointButton(e:MouseEvent):void{
    	addTenPointsTimer.start();
    }
    tenButton.addEventListener(MouseEvent.CLICK, tenPointButton);
    
    
    addTenPointsTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endAddTenPoints);
    function endAddTenPoints(e:TimerEvent):void{
    	addTenPointsTimer.reset();
    	trace("Timer is reset");
    }

  4. #4
    Member
    Join Date
    Dec 2004
    Posts
    68
    I just wanted to thank you again for answering my question and pointing me in the right direction, with further research, I was able to utilize the timer complete event to help with a number of commands.

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