3 Replies Latest reply on Jan 8, 2010 5:08 AM by Ned Murphy

    random pause in a loop

    ctross@ksu.edu

      I have a movie clip that is preforming a repeating earthquake like effect. What I am attempting to do is have the function pause for a random interval

      between 2 and 10 seconds before repeating. This is the script I am using for the "earthquake" repeating effect.

       

      onEnterFrame = function(){

       

      mc._x = Math.random()*4;

       

      mc._alpha = Math.random()*50+50;

       

      mc._rotation = Math.random()*2;

      }

       

      This code is doing what I want it to do but I am unable to find out how to get the random pause to work. I have read through several online forums

      but I have not been able to get anything to work.

       

      please help.

       

       

      ctross.

        • 1. Re: random pause in a loop
          Ned Murphy Adobe Community Professional & MVP

          Here's one approach.  In this version it starts off shaking, and the shaking and non-shaking periods both vary between 2 and 10 seconds.

           

          var quakeNow = true;

           

          function startQuake(){
               var st = setTimeout(doQuake, 2000+Math.round(Math.random()*8000));
               quakeNow = !quakeNow;
          }

           

          function doQuake(){
               if(quakeNow){
                      onEnterFrame = function(){
                            mc._x = Math.random()*4;
                            mc._alpha = Math.random()*50+50;
                            mc._rotation = Math.random()*2;
                      }
               } else {
                      delete onEnterFrame;
               }
               startQuake();
          }

           

          doQuake();

          1 person found this helpful
          • 2. Re: random pause in a loop
            ctross@ksu.edu Level 1

            thanks for the help Ned. The script is doing what I intended but I have found that the random delay is slowing down the quake effect that I am interested. Basically I have a text block I want to shake randomly for 2 seconds then pause.  Is there a way to have the script preform its function for 2 solid second then pause for 5 to 10 seconds since this is not a tween but a function.

             

             

            so something to have it preform the function for 2 seconds

             

             

              onEnterFrame = function(){
                              mc._x = Math.random()*4;
                              mc._alpha = Math.random()*50+50;
                              mc._rotation = Math.random()*2;

             

            then something to pause the function

             

             

            Thanks again

             

            ctross

            • 3. Re: random pause in a loop
              Ned Murphy Adobe Community Professional & MVP

              Yes, there is a way and I am going to only suggest the approach to a solution so that you can try to think it thru yourself and understand better what you currently have.  You will want to change the function below, first by moving the quakeNow = !quakeNow; line to be first.  Then build a conditional that assigns the setTimeout based on the current quakeNow value... one setTimeout() for one value and another setTimeout() for the other.

               

              function startQuake(){
                   var st = setTimeout(doQuake, 2000+Math.round(Math.random()*8000));
                   quakeNow = !quakeNow;
              }

              1 person found this helpful