1 Reply Latest reply on Jul 10, 2006 11:51 AM by blemmo

    Waiting for a movie clip to finish

    CogDev Level 1
      I'm writing code for a simple game, and I want my code to be contained within .as files, and have as little code as possible on the timeline or inside movie clips.

      My main actionscript file controls when and where to play appropriate movie clips, but I've run into a bit of a problem. I have movie clips running on top of one another because the script does not wait for the previous one to finish before executing the next line of code.

      I tried what I thought would be a simple solution:

      while( movieClip._currentframe < movieClip._totalframes) {
      trace(movieClip._currentframe);
      }
      But when that code executes, it causes flash to be unresponsive, and the trace just returns 1 several times, so it never even gets to the second frame of the movie clip.

      I would greatly appreciate any help you may be able to offer.
        • 1. Re: Waiting for a movie clip to finish
          blemmo Level 1
          Right, a 'while' or 'for' keeps the player busy, for the time it executes Flash won't do anything else. For periodical things like the one described, it's better to use onEnterFrame or a function that executes repeatedly with setInterval. OnEnterFrame executes in the speed of the movie's frame rate, so it should be enough for checking the movie progress, because it plays with the same speed.
          So you could use something like this when you attached a movie:
          --
          mainMC.onEnterFrame = function(){
          if (movieClip._currentframe == movieClip._totalframes){
          // reached the last frame, start next mc
          movieClip.removeMovieClip();
          otherMovieClip.play();
          // delete the event
          delete this.onEnterFrame;
          }
          }
          --
          The onEnterFrame could also be attached to the same MC it should watch, like this:
          --
          movieClip.onEnterFrame = function(){
          if (this._currentframe == this._totalframes){
          // reached the last frame, start next mc
          otherMovieClip.play();
          // remove MC, event gets removed as well
          this.removeMovieClip();
          }
          }
          --
          The second way might be better if there are several MCs playing and need to be watched.

          hth,
          blemmo