How to Pause a Timeline (AS2)

ActionScript 2.0 Flash

Note:  An ActionScript 3.0 version of this article is located in a more recent entry of this blog.

The timelines in Flash are an especially good metaphor for tween-based animation.  If your frame rate is the default 12fps, then a span of twelve frames lasts approximately one second.  If your frame rate is 24fps, that same span lasts approximately half a second.  Pretty straightforward.  It can get tedious, however, when you want your animation to pause for, say, 15 seconds before moving on.  At 24fps, you’d need 360 frames!  That’s 360 frames of perhaps a dozen layers.  Ack!  Isn’t there another way to pull off the same visual effect?  Enter, ActionScript. 

An answer, short and sweet

Add a keyframe to the position in your movie where you’d like the timeline to pause.  Any layer will do, but if you like to be organized, create a dedicated scripts layer.  Click into this keyframe and paste the following ActionScript.

stop();
var interval:Number = setInterval(
  function():Void {
    play();
    clearInterval(interval);
  },
  2000
);

How it works

The global stop() function stops the current timeline.  The global setInterval() function waits two seconds, then invokes a function literal that a) causes the current timeline to continue and b) prevents the function from repeating.

setInterval() can be used two ways.  The above approach accepts two parameters:  a function reference and a time interval in milliseconds.  The function reference can be a named function or, as above, a function literal.  It may help to step through how this was “built.”  Bullet #4 would work just fine, by the way.  Line breaks don’t really matter, so long as the punctuation is correct.

  1. setInterval(functionReference, timeInterval);
  2. setInterval(functionReference, 2000);
  3. setInterval(function():Void {}, 2000);
  4. setInterval(function():Void {play(); clearInterval(interval);}, 2000);

So, what’s this clearInterval() business?  Well, any function reference you pass to setInterval() will be invoked repeatedly.  On its own, the play() function would be issued every two seconds, nonstop, which might interfere with other stop() functions in other frames of the movie.

The purpose of the global clearInterval() function is to halt this repetition.  clearInterval() accepts one parameter, a number that identifies which interval (of perhaps many) to halt. And how would you know which number to supply?  Well, thankfully, setInterval() returns a value, which is exactly the number you need.  It’s very much like those “take a number” machines where you grab a piece of paper while waiting your turn.  In the above example, we’re storing that number in a variable, arbitrarily named interval, and using that as our parameter.

Variations

Change the 2000 to whatever you like to adjust the pause length.  This number is in milliseconds, so 15 seconds would be 15000, a minute would be 60000, five minutes would be 300000, and so on.  Half a second would be 500.

The above code works in any timeline, you may use it in the frame of any movie clip to pause that movie clip alone.

Leave a Reply