4 Replies Latest reply on Jan 6, 2008 7:52 AM by clbeech

    Removing movie clips one at a time with delay

    drn24
      tried for 3 hours to make this code work so I wouldn't need to bother anyone here, but apparently I need help...

      Imagine 300 bricks on a wall, each a movie clip. I have manually named 125 of them b1 - b125. What I want to do is on EnterFrame start removing the numbered bricks one at a time with a 1/100th of a second delay between each brick. I am using a for statement which works fine, but not sure how to make the delay inside the loop. Here is the code:

      onEnterFrame = function(){
      for (var i:Number = 0; i < 126; i++) {
      this["b" + i]._alpha = 0; //makes the brick invisible but keeps it on the stage to reappear later.
      loadInt = setInterval(this, "startPause", 20);
      }
      }

      function startPause() {
      clearInterval(loadInt);
      }

      I just can't seem to grasp the setInterval command. I have tried it many times, including using the code exactly from the Adobe LiveDocs, and I can't seem to make it work. Any help would be greatly appreciated.
      Thanks
        • 1. Re: Removing movie clips one at a time with delay
          clbeech Level 3
          instead of using onEnterFrame with a loop, use setInterval itself to handle this procedure and a variable to count through the bricks. setInterval will fire the assigned method once at the time set in the second parameter, in ms. the reason that this is not working, is that the onEnterFrame function is executing over again previous to the setInterval firing (or at least at least a couple of them) in effect 'over-riding' the pause which doesn't 'stop' the onEnterFrame event. for example, if you set the parameter in the setInterval to 1000, it will execute once per second.
          • 2. Re: Removing movie clips one at a time with delay
            drn24 Level 1
            CLBeech, Thank you very much! That script finally worked, but I did have to add an enterFrame function to make it work because the function was not activating when I entered the frame. Here is the code I added:

            onEnterFrame = function () {
            removeBrick();}

            The problem with this is that the bricks unload at the frame rate I have set rather than at the designated interval, so they are very slow. I seem to be missing something. How can I get the removeBrick function to play when I enter the frame without calling out an enterFrame command?

            Thanks for all your help. This is a load off of my mind just to get that code working.
            • 3. Re: Removing movie clips one at a time with delay
              drn24 Level 1
              I did manage to get your code to work clbeech, I just had to move the one line of code to below the function. The moved code was:

              remove_id = setInterval(removeBrick, 10);

              With that change it works perfectly! Thanks for your help.
              • 4. Re: Removing movie clips one at a time with delay
                clbeech Level 3
                very good! yes it was just a general construct to show principle. I would consider placing that line within a method that you can call when needed rather than triggered on entering the frame however. and the same principles will apply to 'reappearing' bricks, of course.

                in addition it would be possible to use the same method for either, by passing a parameter var into the method in the setInterval statement, and use the var to set the alpha value to either 0 or 100.

                also, you're welcome :)