
< Day Day Up >
Writing and Understanding Loop Conditions
For the rest of this lesson, we'll focus on the while loop. The actions within this type of
loop are performed continuously—as long as the condition evaluates to true. For
example:
var i:Number = 0;
while (i < 10) {
// perform these actions
}
The condition in the loop is i < 10. This means that as long as the value of i is less than
10, the statement is true and the actions within the loop are repeated. However, the
looping statement is missing a key ingredient; it doesn't have a means by which the
condition eventually becomes false. Without this functionality, the loop could continue
forever, and in the real world endless loops cause applications to freeze. Flash can't do
anything else until the looping statement completes its job. To prevent an endless loop in
the example, the value of i must be incremented so that its value eventually equals 10, at
which point the condition proves false and the loop stops.
You can use the increment operator (++) to handle incrementing a loop. Here's an
example:
var i:Number = 0;
while (i < 10) {
//perform these actions
++i
}

Incrementing the value of i causes the loop to perform 10 iterations. The value of i is
initially set to 0. However, with each loop that value increases by 1. On the tenth loop, i =
10, which means that i < 10 is no longer true and the loop halts. Here's a shortcut that
accomplishes the same goal:
var i:Number = 0;
while (++i < 10) {
// perform these actions
}
This loop performs 9 iterations. The value of i is initially set to 0. However, with each
iteration (including the first), that value is incremented by 1 within the conditional
statement of the loop itself. On the tenth loop, i = 10, which means that i < 10 is no
longer true, and the loop halts.

You can also use the decrement operator with a loop, which might look something like
this:
var i:Number = 10;
while (--i > 0) {
// perform these actions}
}
Alternatively, you can write this script using a for loop as follows:
for (var i:Number = 10; i>0; --i) {

// perform these actions
}
The condition in a loop doesn't have to depend on an incremented value; it can be any
sort of condition. It can also be the result of a function call that returns a value of true or
false, like this:
while (someFunction()) {
// perform these actions
}
In the following exercise, you create a drop-down list using a while loop.
1. Open pictureShow1.fla in the Lesson09/Assets folder.
The main timeline includes three layers: Background, Dynamic Elements, and
Actions. The Actions layer contains all the ActionScript for this project. Not
surprisingly, the Background layer contains the project's background graphic. The
Dynamic Elements layer includes four movie clip instances: three above the stage
that contain pictures, and an instance on the stage named dropDownList_mc that
contains a button named menu_btn and another movie clip instance named
item_mc. The item_mc instance is made up of two elements: a dynamic text field
with an instance name of itemName_txt, and a button that appears as a
semitransparent white box and has an instance name of list_btn. The
itemName_txt instance plays an important part in this exercise because it will be
duplicated in a process to generate clickable menu choices dynamically.

2. With the Actions panel open, select the frame in the Actions layer and add these
two lines:
3.
4. var buttonNames:Array = ["Paris", "New York", "London"];
5.
6. dropDownList_mc.item_mc._visible = false;
7.
The first action in this script creates an array named buttonNames, which contains
names that will appear in the drop-down list. The second action makes the
item_mc movie clip instance invisible. (Because you'll be using the item_mc
movie clip instance only as a template for creating duplicates, the instance itself
doesn't need to be visible.)
3. Add this function after the script you just added in Step 2:
4.
5. function populateList() {
6.
7. var spacing:Number = dropDownList_mc.item_mc._height + 2;
8.
9. var numberOfButtons:Number = buttonNames.length;
10.
11. }
12.
When complete, this function duplicates the item_mc movie clip instance, aligns
the duplicates under the Menu button, and changes the text displayed in the