1 Reply Latest reply on Aug 5, 2010 4:41 PM by vezey

    Issue timer air not accurate

    flexJu

           Hello,

      I want to use the Timer class for a soft I am developping.

      I encounter an issue while starting two timers at the same time. The two timers are not sampled identically:

      For example if I start a timer with a delay of 1000 and another with a timer of 4000, they must fire together once in four step.

      But after some iteraction, they stop to fire synchronously, as if they were not synchronized anymore.

       

      As sample code is more efficient than long talk, I post a little code showing my issue:

       

      public var delay:Number=1000;

      public var delay2:Number=4000;

      public var repeat:Number=100;

      public var myTimer:Timer=new Timer(delay,repeat);

      public var myTimer2:Timer=new Timer(delay2,repeat*delay/delay2);

       

       

      protected function button1_clickHandler(event:MouseEvent):void

      {

      myTimer.addEventListener(TimerEvent.TIMER_COMPLETE,timer1);

      myTimer2.addEventListener(TimerEvent.TIMER, timeHandler2);

      myTimer2.addEventListener(TimerEvent.TIMER_COMPLETE,timer2);

      myTimer.addEventListener(TimerEvent.TIMER, timeHandler);

      myTimer.start();

      myTimer2.start();

      }

      //function handling the move of the timecurser

      protected function timer1(e:TimerEvent):void

      {

      Alert.show("Timer "+delay/100+"s stopped","Timer Stop");

      }

      protected function timer2(e:TimerEvent):void

      {

      Alert.show("Timer "+delay2/100+"s stopped","Timer Stop");

      }

      protected function timeHandler(e:TimerEvent):void

      {

      but1.x+=delay/myTimer.repeatCount;

      trace(e.currentTarget.currentCount*delay/1000);

       

      }

      protected function timeHandler2(e:TimerEvent):void

      {

      but2.x+=delay2/myTimer.repeatCount;

      trace(e.currentTarget.currentCount*delay2/1000);

       

      }

      public var delay:Number=1000;

      public var delay2:Number=4000;

      public var repeat:Number=100;

      public var myTimer:Timer=new Timer(delay,repeat);

      public var myTimer2:Timer=new Timer(delay2,repeat*delay/delay2);

       

       

      protected function button1_clickHandler(event:MouseEvent):void

      {

      myTimer.addEventListener(TimerEvent.TIMER_COMPLETE,timer1);

      myTimer2.addEventListener(TimerEvent.TIMER, timeHandler2);

      myTimer2.addEventListener(TimerEvent.TIMER_COMPLETE,timer2);

      myTimer.addEventListener(TimerEvent.TIMER, timeHandler);

      myTimer.start();

      myTimer2.start();

      }

      //function handling the move of the timecurser

      protected function timer1(e:TimerEvent):void

      {

      Alert.show("Timer "+delay/100+"s stopped","Timer Stop");

      }

      protected function timer2(e:TimerEvent):void

      {

      Alert.show("Timer "+delay2/100+"s stopped","Timer Stop");

      }

      protected function timeHandler(e:TimerEvent):void

      {

      but1.x+=delay/myTimer.repeatCount;

      trace(e.currentTarget.currentCount*delay/1000);

       

      }

      protected function timeHandler2(e:TimerEvent):void

      {

      but2.x+=delay2/myTimer.repeatCount;

      trace(e.currentTarget.currentCount*delay2/1000);

       

      }

      the two buttons go forward every time the timer fire.

       

      After somme iteration, the delay2 button doesn't go forward syncrhonously with the delay1 button.

       

      I can't see a problem with my code so maybe it is a bug in Air, or maybe I didn't see something.

       

      Regards

      Julien

        • 1. Re: Issue timer air not accurate
          vezey

          Julien,

           

          The API Docs have this to say in the introduction of the Timer class:

           

          You can create Timer objects to run once or repeat at specified intervals to execute code on a schedule.    Depending on the SWF file's framerate or Flash Player's environment (available   memory and other factors), Flash Player or Adobe AIR may dispatch events at slightly   offset intervals. For example, if a SWF file is set to play at 10 frames per second (fps), which is 100 millisecond   intervals, but your timer is set to fire an event at 80 milliseconds, the event will be dispatched close to the   100 millisecond interval. Memory-intensive scripts may also offset the events.

           


          This does not exactly apply to your situation, but if I understand how the code execution works, the two Timer instances can't begin exactly at the same time, but they will be created and begin very close to one another. It sort of makes sense that over time, they will grow out of synch, given that their environment is not crystalline pure. You might be able to check every so often and move them back into synch, however.

           

          Bill