Flylib.com

Books Software

 
 
 

What You Will Learn


What You Will Learn

In this lesson, you will:

  • Discover the usefulness of loops

  • Learn about the types of loops

  • Examine loop exceptions

  • Set loop conditions

  • Create a nested loop



Approximate Time

This lesson takes approximately forty-five minutes to complete.



Lesson Files

Media Files:

None

Starting Files:

Lesson04/Start/pictureShow1.fla

Lesson04/Start/phoneNumberSearch1.fla

Completed Projects:

Lesson04/Completed/pictureShow3.fla

Lesson04/Completed/phoneNumberSearch2.fla



Why Loops Are Useful

Loops enable Flash to perform an action (or set of actions) repeatedly, which means that with just a few lines of ActionScript, you can force an action to be executed several thousand times. In ActionScript, you use loops for tasks that would be difficult or impossible without loops. For example:

  • Creating dynamically generated drop-down lists

  • Validating data

  • Searching text

  • Dynamically duplicating movie clips

  • Copying the contents of one array to a new array

You can use loops to automate any number of tasks, such as dynamically creating MovieClip instances. Suppose that your project called for 100 evenly spaced instances of the same MovieClip. You could create a four- or five-line looping statement to attach a MovieClip from your library 100 times and position each clip on the Stage automaticallya great improvement over dragging 100 instances from the library and aligning each one manually.

Loops are also dynamic. Suppose that you scripted a loop to create a menu of 15 choices ( buttons ) dynamically. By altering the loop slightly, you could add and remove choices dynamically. This would be impossible to do without loops.

As you progress through this lesson (and the rest of the book), you'll see the value of using loops in your scripts.



Types of Loops

ActionScript can take advantage of three loop types, all of which repeat an action (or set of actions).

while Loop

The syntax for creating this common type of loop is as follows :

while(someNumber < 10) {
     // perform these actions
   }


The expression someNumber < 10 is the condition that determines how long the loop will iterate (pass through the statement). The actions inside the loop are executed with each iteration. The logic that determines how the condition is evaluated (and how the loop is exited) is discussed shortly, in the section "Writing and Understanding Loop Conditions."

Here's an example of the while loop :

while(myClip._y < 0) {
     myClip._y += 3;
   }


This script moves the myClip MovieClip instance along the y axis until its position is greater than or equal to .

You could also use a while loop to loop through an array like this:

var data:Array = ["one", "two", "three"];
   var i:Number = 0;
   while(i < data.length) {
     trace(data[i++]);
   }


In the next section we will look at a more common way to iterate over predefined number of items, such as an array.

for Loop

The for loop is a compact, all-in-one looping solution for loops that rely on incrementing or decrementing a variable. The for loop lets you initialize a loop variable, set the loop condition, and increment or decrement that variableall in one line of ActionScript. The for loop is typically used to perform an action or set of actions based on the value of an incremental variablewalking an array, for example, or applying actions to a list of movie clips. Here's the syntax of the for loop:

for(var someNumber:Number = 0; someNumber < 10; someNumber++) {
       // perform these actions
     }


The three elements separated by semicolons within the parentheses are used to specify the number of iterations the loop will perform. In this example, the variable someNumber is created and assigned an initial value of . The script states next that as long as someNumber is less than 10 , the loop executes the actions contained in the loop. The last element in the parentheses specifies that someNumber will be incremented by 1 with each loop iteration, eventually causing someNumber to have a value of 10 , which means that the loop will cease after 10 iterations.

The for loop is structured to be used primarily to loop through a set of actions a specified number of times. Here's the same example given for the earlier while loop, but now using the for loop syntax:

var data:Array = ["one", "two", "three"];
    for(var i:Number = 0; i < data.length ; i++) {
      trace(data[i]);
    }


for...in Loop

This loop is used to iterate through all of an object's properties. Here's the syntax:

for(var key:String in someObject) {
      trace(key);
    }


The key in the loop is a variable that temporarily stores the name of the property referenced by the variable with each loop iteration. The value of key can be used in the actions within the loop. For example, let's say you had the following car object in your application:

var car:Object = new Object();
    car.color = "red";
    car.make = "BMW";
    car.doors = 2;


But there's a problem: You aren't aware of the properties the car object might have. This could be because your car object is dynamic, meaning the properties can be added at runtime. Therefore, you need a way to inspect the object and retrieve all the properties. The following script will iterate through the object and output the key and values for each property on the car object:

for(var key:String in car) {
      trace(key + ": " + car[key]);
    }


On the first iteration of the loop, key has a String value of doors (because that was the name of the last property defined). During the first loop, the output looks like this:

"doors: 2";


In the expression we trace the output; the variable key (without brackets) refers to the property name (such as doors , make , or color ). Using car[key] (that is, placing key variable between brackets) is the same as writing car.doors and is a reference to that property's value .

When the loop is complete, the output panel has the following:

doors: 2
   make: BMW
   color: red


Because the car object has three properties, the for...in loop in this script will perform only three iterations.

Note

In a regular array, elements are referenced by number, starting with 0. In contrast, elements in an associative array are referenced by name. The for...in loop in this section loops through the associative array that contains all of these references in a specific Timeline or object.