2 Replies Latest reply on Sep 18, 2007 6:36 PM by SephXI

    Timer class not being

    SephXI
      Hi all, new to Flex so having a few problems. This one should be straight forward, so it's frustrating that I can't figure out whats wrong.

      I'm trying to set timers to loop through several application states. I use the (corrected) example timer code from the Adobe docs to trigger a currentState change but doesn't seem to work.

      Simplified:


      main.mxml

      public function initApp():void {
      ShortTimer(0);
      }

      public function colstate():void {
      currentState = 'state2';
      }

      ShortTimer.as

      package
      {
      import flash.display.Sprite;
      import flash.events.Event;
      import flash.events.TimerEvent;
      import flash.utils.Timer;
      import mx.core.Application;

      public class ShortTimer extends Sprite
      {
      public function ShortTimer()
      {
      var minuteTimer:Timer = new Timer(1000, 3);
      minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
      minuteTimer.start();
      }

      public function onTimerComplete(evt:Event):void
      {
      Application.application.colstate();
      }
      }
      }


      I've tried Application.application.currentState='state2'; to no avail. I also tried calling the currentState change at the very beginning of the ShortTimer function, and it still wasn't recognised.

      Having the ShortTimer code inline with the main.mxml rather than a separate class file makes no difference either.

      What am I missing?
        • 1. Re: Timer class not being
          TeotiGraphix Level 3
          Hi,

          The problem is your timer var is going out of scope when the function exits.

          You need

          private var minuteTimer:Timer;

          public function ShortTimer()
          {
          minuteTimer= new Timer(1000, 3);
          minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
          minuteTimer.start();
          }

          Peace, Mike
          • 2. Re: Timer class not being
            SephXI Level 1
            Thanks for your help!

            I had another error elsewhere in my code too, but luckily I figured that one out. The private variable would've caused a lot of grief, though!

            Damien