Many things in Flash will need to loop to make something happen. A preloader, for example. This is a looping movie or game that plays while the rest of the movie is loading. The looping is an important part of continually checking to see if a part of the movie is loaded.
It is also very useful for telling many movie clips to do the same thing at once. You can do this by naming your movie clips numerically, as in MyClip1, MyClip2, MyClip3,etc.
Looping either occurs through multiple frame loops or through single frame loops.
Basically, loops tell Flash to go back to whatever it was just doing and do it again.
Often loops are used to increment a variable within the action, changing each time the action is invoked to add an amount to the variable value.
The most basic form of a Multiple-Frame Loop needs only two frames. One frame to apply an action, and a second frame to tell Flash to go back and invoke that action again.
This is used often in preloaders.
A preloaders multiframe loop can be found by downloading the
simple preloader (It is in Scene 1 or this two scene file)
The code on frame 2 looks like the following:
if (btload>=totbt) {
gotoAndPlay ("main", 1);
//trace ("loading complete");
} else {
gotoAndPlay (1);
}
This means, if the movie is loaded, go and play the main scene, frame 1. If not, go and play frame 1 again. It plays frame 1 and returns to this questioning script until it is loaded.
Single frame Loops can be more complicated. The Single-Frame Loop uses the While, For, do...while actions.
Another way to continually check for a condition, is with onClipEvent. With onClipEvent you can attach scripts to movie clip instances and you can check for conditions, such as the mouse being moved, or pressed, or if external variables are loaded in from a server, or if a loaded movie (another swf file) is fully loaded before it starts playing.
These actions works much the same way as the multiple-frame loop, except that they repeat their action in the course of a single-frame.
Because they performs within a single frame, their actions are time independent. Whereas the multiple-frame loop is bound by the movement of the playback head and the frame rate of the movie.
ex. of a while loop
From the file shown below:
n = 1;
while (n<20) {
duplicateMovieClip ("dog", n, n);
setProperty (n, _x, random(760));
setProperty (n, _y, random(390))
n = n+1; //n+=1
}
download the looping file shown
above (file name: loop.fla)
right click on the file (control-click for Mac) and select to download to your
drive.