Pausing a Macromedia Flash 5 movie for a given number of seconds
This TechNote provides a method for looping over a range of frames for a given amount of time. It might be tempting to use awhileorforloop to achieve this end, but this is generally bad programming practice. A script executes until finished, and if you stay within one script for multiple seconds at a time, then the computer is not able to perform other tasks such as refresh the display or check for mouse events. In some cases it might 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.
// 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 will first check whether the variable namedstartTimehas been initialized, and if not will set it to the current time. Each time the playback head reaches the frame containing the script it will check whether the elapsed time has exceeded the desired pause duration, looping 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 two variables at the top of the script for how long the loop should continue (pauseDuration) and how many frames are included in the loop (framesInLoop).
For example, the sample script above would cause the playback head to loop over 10 frames for 5 seconds (5 * 1000 milliseconds). Placing this script on a keyframe at frame 20 would cause the playback head to loop over frames 10-20 for 5 seconds before continuing on in the timeline.
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 will work only in the first scene. The reason is that the_currentFrameproperty is not scene specific. For example, if a movie contains three scenes each containing ten frames,_currentFramewould evaluate to "30" on the last frame of scene three, rather than "10". ThegotoAndPlay()function is scene specific, however.
Related TechNotes:
| |
How can I loop over some frames a given number of times? (TechNote 14023) explains how to to repeat a series of frames by number, rather than by time, in Flash 4. |
| |
How to use GetTimer to pause a movie (TechNote 14207) explains how to make a Movie Clip timer in Flash 4. |
| |
What causes a 'more than 200000' actions error? (TechNote 14570) discusses the danger of using very long loops. |
| |
How can I prevent mouse clicks from executing? (TechNote 12490) discusses similar principles in Director. |
This content requires Flash
To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.
Download the free Flash Player now!
