5 Replies Latest reply on Apr 20, 2010 12:13 PM by kglad

    Clearing an interval

    Grif_22

      I have a small game I am developing. To mimic levels I have a series of setIntervals which run at different times. They look like this:

       

      var level1Timer = setInterval(generateEnemies, 2000, 2, 100, randomSpeed + 3, 0xccccff, "level1Timer");

       

      The parameters pass into a function which looks like this:

       

      function generateEnemies(noEn, trans, speed, color, level)

       

      The point in question is the last parameter, level. The reason I am passing this through is so that after the interval has run, I can then clear it so that it does not keep looping. To do this I am using just

       

      clearInterval(level);

       

      However this does not work. During some error testing, I noticed that if I passed in the parameter 'level1Timer' BUT WITHOUT quotes around, displaying the value showed undefined. As I am still learning, I can only assume this happens because without quotes it attempts to pass through a variable called level1Timer, and not the value as a string. Hence, this is why I have put quotes around now and this does indeed display as normal. Problem now is it does not seem to clear, possibly because it has been converted into a string??

       

      I know the clear definitely works as if I put in:

       

      clearInterval(level1Timer);

       

      it works.

       

      Hope someone can help,

       

      Chris

        • 1. Re: Clearing an interval
          kglad Adobe Community Professional & MVP

          use the array operator to have flash coerce your string to an object.

           

           

          var tl:MovieClip=this;

          function generateEnemies(noEn, trans, speed, color, level){

          clearInterval(tl[level])

          • 2. Re: Clearing an interval
            Grif_22 Level 1

            Perfect. Works a treat!

             

            Don't suppose you could explain that slightly more though could you? I didn't really understand this:

             

            "have flash coerce your string to an object"

             

            Just for future reference!

             

            Many thanks,

             

            Chris

            • 3. Re: Clearing an interval
              kglad Adobe Community Professional & MVP

              your level parameter is a string.  it's not a variable.  flash is too stupid to realize you want it (flash) to be treated like a variable.  you can instruct flash to treat it like a variable by using the array operator scopeobject(a left bracket, your string, right bracket).

               

              the scopeobject is the scope in which your variable is defined.  your variable is defined in the timeline where you have that code:

               

              var tl:MovieClip=this;  // tl gives a reference to the scope of your variable

               

              var yourvariable:Number = setInterval(whatever,...)

               

              tl["yourvariable"]  is a reference to the variable, yourvariable

               

              "yourvariable" is just a string to flash.  it doesn't see any similarity between "yourvariable" and yourvariable.  those two things look like an apple and an orange to flash.  but when you use:

               

              tl["yourvariable"]

               

              flash sees tl.yourvariable (assuming such a thing exists).

              1 person found this helpful
              • 4. Re: Clearing an interval
                Grif_22 Level 1

                And an excellent explanation as well! Thank you very much, 5*!

                 

                Chris

                • 5. Re: Clearing an interval
                  kglad Adobe Community Professional & MVP

                  you're welcome.