A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Problem with loop and timer CS5.5

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    12

    Problem with loop and timer CS5.5

    I have a For loop set up but I want a pause every increment, so I set up the below code using a timer and a boolean variable to try and force the parser (if that's the correct word) to run through 50 ticks of the timer each increment of the loop. Problem is, while the timer is running the For loop still continues.

    Code:
    for(i = 0; i < 100; i++){
    	      if (flag == true){
    	      myTimerTest.reset(); myTimerTest.start();
    	 }
    	  else{
    		RepairWall();
    		flag = true;
    	}
    	}
    			
    	function timerTestHandler(event:TimerEvent){
               if (myTimerTest.currentCount==50){
               myTimerTest.stop();
               flag = false;
               }
    	 }
    	
          function RepairWall():void{
              // do stuff using i variable
          }
    (this isn't the complete program btw, just the relevant code.)

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

    Does this help ?
    PHP Code:
    var myTimerTest:Timer = new Timer(1);
    var 
    i:Number 0;

    myTimerTest.start();
    myTimerTest.addEventListener(TimerEvent.TIMER,timerTestHandler);

    function 
    timerTestHandler(event:TimerEvent)
    {
        
    trace(myTimerTest.currentCount);
        if (
    myTimerTest.currentCount == 50)
        {
            
    //trace(i + " Finished");
            
    RepairWall();
            if ( 
    100)
            {
                
    i++;
                
    myTimerTest.reset();
                
    myTimerTest.start();
            }
            else
            {
                
    myTimerTest.stop();
                
    myTimerTest.removeEventListener(TimerEvent.TIMER,timerTestHandler);
            }
        }
    }

    function 
    RepairWall():void
    {
        
    trace("Timer " " finished - Repaired wall " i);


  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I notice you said have a delay before next run through, you could always do it like this then
    PHP Code:
    var myTimerTest:Timer = new Timer(1);
    var 
    spinTimes:Number 3;
    var 
    i:Number 0;

    myTimerTest.start();
    myTimerTest.addEventListener(TimerEvent.TIMER,timerTestHandler);

    function 
    timerTestHandler(event:TimerEvent)
    {
        
    trace(myTimerTest.currentCount);
        if (
    myTimerTest.currentCount == 50)
        {
            
    trace("Timer " " finished");
            
    trace("Wait 1 second for next run");
            
    myTimerTest.stop();
            
    myTimerTest.reset();
            
    setTimeout(RepairWall1000);
        }
    }

    function 
    RepairWall():void
    {
        
    trace("Repaired wall " i);
        if ( 
    spinTimes)
        {
            
    i++;
            
    myTimerTest.start();
        }
        else
        {
            
    myTimerTest.stop();
            
    myTimerTest.reset();
            
    myTimerTest.removeEventListener(TimerEvent.TIMER,timerTestHandler);
        }


  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    12
    That helped thanks

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