4 Replies Latest reply on May 13, 2009 6:50 AM by IlyaG

    how do I use time events?

    IlyaG Level 1

      I been trying to create a time event function but some thing got wrong, check this out:

       

      public function randomNPCMove(t:TimerEvent):void

      {                      

                  var NPCMoveDelay:Timer=new Timer(Math.random()*1000,1);
                  NPCMoveDelay.addEventListener(TimerEvent.TIMER_COMPLETE,randomNPCMove);

                  Alert.show("I am alive");

      }

        • 1. Re: how do I use time events?
          Barna Biro Level 3

          Hi there,

           

          I can't say it's wrong but it's definitely weird. You would define your Timer object outside the method. Once defined, instantiated and the timer listener added, you need to start it with NPCMoveDelay.start(); once it's started, it will keep calling the randomNPCMove ( you specify the repeat count upon instantiation ).

           

          If you want to stop the timer at any point, you simply call NPCMoveDelay.stop(); By the way, you need to add a TimerEvent.TIMER listener in order for your randomNPCMove method to be triggered every time the timer is triggered. TimerEvent.TIMER_COMPLETE is only triggered when the timer has reached the last repeat and stops.

           

          Read up on Timers for more info: http://livedocs.adobe.com/flex/3/html/help.html?content=08_Dates_and_times_4.html

           

          With best regards,

          Barna Biro

          Blog: http://blog.wisebisoft.com

          • 2. Re: how do I use time events?
            Gregory Lafrance Level 6

            A simple app showing Timer in action:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
              creationComplete="timer.addEventListener(TimerEvent.TIMER, timerFunc);">
              <mx:Script>
                <![CDATA[
                  private var timer:Timer = new Timer(500, 0);
                  [Bindable] private var num:uint = 0;
                  
                  private function timerFunc(event:TimerEvent):void{
                    num++;
                  }
                ]]>
              </mx:Script>
              <mx:Label id="lbl" text="{num}" fontSize="50" textAlign="center"/>
              <mx:Button label="Start Timer" click="timer.start();"/>
              <mx:Button label="Stop Timer" click="timer.stop();num=0;"/>
            </mx:Application>
            

             

            If this post helps, please mark it as such.

            1 person found this helpful
            • 3. Re: how do I use time events?
              BennyBeta Level 4

              The previous replier was correct in that you'll need to call start() on your Timer.  You'll also want to move your timer variable (NPCMoveDelay) and Timer creation outside of the function.  You're currently declaring it as a function variable which will disappear when the function is done.

               

              This Timer documentation may be helpful, check out the examples:

              http://www.flexafterdark.com/docs/ActionScript-Timer

               

              I hope that helps...

               

              Ben

              • 4. Re: how do I use time events?
                IlyaG Level 1

                Yes, now this is working. My mistake was that I didn't used Timer.start() function. Now this is working fine.