Flash ActionScript loops

By Riyadh Al Balushi (Blue Chi) | Flash 8 | Beginner

ActionScript loops are tools used to execute a segment of code repeatedly for a number of times or while a certain condition is satisfied. This can save time and effort by not having to type the same code multiple times to repeat a process.

For example, if we wanted to duplicate a movie ten times, without a loop, we would have to type the same code ten times.

duplicateMovieClip ("movie_mc", "new_1", 1);
duplicateMovieClip ("movie_mc", "new_2", 2);
duplicateMovieClip ("movie_mc", "new_3", 3);
duplicateMovieClip ("movie_mc", "new_4", 4);
duplicateMovieClip ("movie_mc", "new_5", 5);
duplicateMovieClip ("movie_mc", "new_6", 6);
duplicateMovieClip ("movie_mc", "new_7", 7);
duplicateMovieClip ("movie_mc", "new_8", 8);
duplicateMovieClip ("movie_mc", "new_9", 9);
duplicateMovieClip ("movie_mc", "new_10", 10);

Using a loop to do this would reduce the amount of code significantly while generating the same identical result. Below is an example of the same command to duplicate a movie ten times using a loop.

for (i=1;i<11;i++){
duplicateMovieClip("movie_mc", "new_"+i, i);
}

loop Types

There are three main types of ActionScript loops, the for loop (which we used in the example above), the while loop, and the do-while loop. The for and while loops do exactly the same thing but with different syntax, the do-while loop differs in one small aspect. We will go through all of these types in turn.

The For loop

I think that the For loop is the most commonly used because it is the most compact and the easiest to understand and use. You can use this loop to execute a number of code statements multiple times using this format:

for (counter; condition; action){
statements;
}

The example above shows the syntax used, the counter (i) sets the starting point for your counter, the condition will determine the point at which the loop will have to stop, and the action operate counter to eventually make it make the condition untrue to stop the loop. The code to be repeated is placed with in the curly brackets.

For example, if we want to output some text ten times, we can do it this way.

for (i=1; i<11; i++){
trace ("This code is repeated ten times");
}

In regular words the code above says, start counting i from 1, repeat the code below as long as i is less than 11, and add 1 to i each time the loop is repeated. We start with i as 1, when the code is repeated the first time then the value of i increases by 1 making it equal 2, when the code is repeated again it becomes 3, then 4, 5, 6, 7, 8, 9, and 10. After that it becomes 11, and at that moment the condition is no longer satisfied because i is not less than 11 (i>11), and so the loop ends at that point.

The While loop

The while loop repeats a set of code as long as the condition specified is true.

while (condition) {
statements
}

This looks very similar to a conditional, which execute a code once if the condition is satisfied, but here the code is executed repeatedly instead of just once. If the condition remained true forever, this means the loop will be repeated over and over again forever as well, and that should be avoided at all costs, because an infinite loop will crash the Flash player. So there is usually something within the statements that eventually makes the condition untrue.

var i = 1;
while (i < 5){
trace ("This code is repeated");
i++;
}

In the example above, the code will get repeated as long as i is less than 5, we made sure that i increases by 1 each time the loop is cycled through.

The Do-While loop

This type of loop runs the code to be repeated before checking for the condition so that it guarantees that the code is executed at least once whether the condition is satisfied or not. It is used in the following format:

do {
statements;
} while (condition);

This loop could just be used like the previous two as shown in the example here.

var i = 1;
do {
trace ("This code is repeated");
i++;
}while(i<5);

The upper example is exactly the same as the one we used for the while loop, but this one would execute the code once even if i was never less than 5 (for example, if we set its value of i as 10 from the start).

Practical Examples of loop Usage

loops are very often used to manipulate and access the content of arrays. Arrays are lists of data under which each item is identified by its order within the list. It is possible to extract all the contents of an array using a simple for loop as illustrated in the example below.

var oman3d = ["home", "tutorials", "features", "competitions", "forum"];
for (i=0; i<oman3d.length ; i++){
trace (oman3d[i]);
}

The code above cycles through the array as long as i does not exceed the total number of items within an array (its length). The code should be self-explanatory if you understand the basics of arrays.

Another common use of loops involves the creation and control of dynamic movieclips. While the process for creating such movieclips is quite easy using a loop, referencing to these movieclips to control them later on can be quite tricky. The easiest method for achieving that is by using the square brackets [] to generate those references dynamically. The example below shows you how 5 movieclips are created and then positioned next to each other using a loop. The name of each movieclip is generated in the second line and called back in the third line using the square brackets.

for (i=1;i<6;i++){
duplicateMovieClip("movie_mc", "new_"+i, i);
this["new_"+i]._x=this["new_"+i]._width*i;
}

You can alternatively save the reference to the movieclip in a temporary local variable which you can use within the loop to easily refer to your various movieclips in turn.

for (i=1;i<6;i++){
duplicateMovieClip("movie_mc", "new_"+i, i);
temp_mc = this["new_"+i];
temp_mc._x=temp_mc._width*i;
}

A more detailed practical example in which both of these techniques are used can be seen in our Interactive Map Tutorial, you should check it out to see how useful loops can be.

This concludes our tutorial, I hope that you learnt the basics on how to use Flash loops, please feel free to post any questions you have at the Oman3D Forum.

- End of this tutorial.