6 Replies Latest reply on Mar 17, 2010 7:42 PM by Harry Kunz

    How to make a timer?

    TheBrody

      I'm trying to make a timer..

       

      When I press a button a movieclip does this: gotoAndStop(2);

      I want it to go back to Keyframe 1 after 10 seconds.

       

      How can I get this to work?

        • 1. Re: How to make a timer?
          Ned Murphy Adobe Community Professional & MVP

          You can use the setTimeout() function to delay a call to a function.

          • 2. Re: How to make a timer?
            TheBrody Level 1

            How can I use that to make it do it after 10 seconds?

            • 3. Re: How to make a timer?
              Ned Murphy Adobe Community Professional & MVP

              Have you looked it up in the help documentation?  I am sure it is all explained there for you--absolutely certain.

              • 4. Re: How to make a timer?
                Harry Kunz Level 4

                If you still didn't find what your looking for you can use this:

                 

                 

                import mx.utils.Delegate;

                var nDuration:Number = 10000; //milliseconds = 10 sec

                var nId:Number = setTimeout(Delegate.create(this, myCallbackFunc), nDuration);

                 

                function myCallbackFunc( ):Void

                {

                    trace("Timer Expired with id: " + nId);

                }

                • 5. Re: How to make a timer?
                  TheBrody Level 1

                  Thanks...

                   

                  I got another question though..

                   

                  I have it to when you press a tree (button) it calls a function.. But I have multiple trees that call the function..

                   

                  I want the function to make the timer for just that tree.. I tried this:

                   

                  Button1.onRelease = function() {

                  TreeNum = 1;

                  ResetTree();

                  }


                  Button2.onRelease = function() {

                  TreeNum = 2;

                  ResetTree();

                  }


                  function ResetTree(){


                       import mx.utils.Delegate;

                       var nDuration:Number = 5000; //milliseconds = 10 sec

                       var nId:Number = setTimeout(Delegate.create(this, myCallbackFunc), nDuration);

                   

                  function myCallbackFunc( ):Void

                   

                  {

                   

                      _root.TheMap["TreeCuts" + TreeNum].gotoAndStop(1);
                  }

                  }

                   

                  But if I press 1 button then another one before 5 seconds... The first buton won't go back to frame 1.. How can I fix this?

                  • 6. Re: How to make a timer?
                    Harry Kunz Level 4

                    First, place imports always at the top of your codes. You can fix the problem with a queue:

                     

                    import mx.utils.Delegate;

                     

                    var aTreeQueue:Array = new Array();

                     

                    Button1.onRelease = function() {

                        TreeNum = 1;

                        aTreeQueue.push(TreeNum);

                        ResetTree();

                    };

                     

                    Button2.onRelease = function() {

                        TreeNum = 2;

                        aTreeQueue.push(TreeNum);

                        ResetTree();

                    };

                     

                    function ResetTree() {

                        var nDuration:Number = 2000;//milliseconds = 10 sec

                        var nId:Number = setTimeout(Delegate.create(this, myCallbackFunc), nDuration);

                    }

                     

                    function myCallbackFunc():Void {

                        var nTree:Number = aTreeQueue.splice(0,1)[0]; //FIFO (first in first out)

                        _root.TheMap["TreeCuts"+nTree].gotoAndStop(1);

                    }