Pause SWF file | Specific number of secondsProducts affected
This TechNote provides an ActionScript 2.0 method for looping over a range of frames for a given amount of time. It can be tempting to use awhileorforloop to achieve this end, but this method is generally a bad programming practice. A script executes until finished. If you stay within one script for multiple seconds, then the computer can't perform other tasks such as refresh the display or check for mouse events. In some cases, it can appear as if the computer crashed.
To stay in a range of frames for a given number of seconds, consider using something like the following script. Instead of staying stuck in one loop, it repeats over a series of frames, checking for whether a condition is fulfilled before proceeding.
Note: The code provided below is ActionScript 2.0. ActionScript 2.0 and 3.0 are incompatible with each other.
// Set the following two values for how long you want to
// loop, and how many frames to loop:
pauseDuration = 5 * 1000;
framesInLoop = 10;
if (startTime == null)
{
startTime = getTimer();
gotoAndPlay(_currentframe - framesInLoop);
}
else
{
lapsedTime = getTimer() - startTime;
if (lapsedTime < pauseDuration)
{
gotoAndPlay(_currentframe - framesInLoop);
}
else
{
startTime = null;
}
}
When the script runs, it first checks whether the variable named startTime has been initialized. If not, tt sets it to the current time. Each time the playhead reaches the frame containing the script, it checks whether the elapsed time has exceeded the desired pause duration. It loops back if the time has not yet been reached.
To use this script, enter (or copy and paste) the code example above into the last frame of the range of frames included in the loop. Then, set the variables for how long the loop continues (pauseDuration) and how many frames are included in the loop (framesInLoop). These variables appear at the top of the script.
For example, the sample script above causes the playhead to loop over ten frames for 5 seconds (5 * 1000 milliseconds). Placing this script on a keyframe at frame 20 causes the playhead to loop over frames 10-20 for 5 seconds before continuing.
In summary, a script executes until done--nothing else can happen while it's stuck in awhileorforloop. For perceptible delays, try regularly testing for a condition in an open frame loop, instead of staying stuck in a tight scripting loop.
Note: In a multi-scene movie, this code works only in the first scene. The reason is that the _currentFrame property is not scene-specific. For example, if a movie contains three scenes each containing ten frames, _currentFrame evaluates to "30" on the last frame of scene three, rather than "10." The gotoAndPlay() function is scene-specific, however.
Related content
How can I loop over some frames a given number of times? (tn_14023)
Doc ID
(tn_14809)
Last updated
2011-05-31
Products affected
