TYPES OF LOOPS


ActionScript can take advantage of three loop types all of which perform an action (or set of actions) while a specific condition is true .

while Loop

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

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

The expression someNumber < 10 is the condition that determines how many iterations the loop will perform. With each iteration (pass through the statement), the actions inside the loop are executed. The logic that determines how the condition is evaluated (and thus how the loop is exited) is discussed in the next section, "Writing and Understanding Loop Conditions." Here is an example of the while loop:

 i=0;  while (++i <= 10) {    myClip.duplicateMovieClip("myClip" + i, i);  } 

The script above duplicates myClip 10 times.

for Loop

The for loop is a compact, all-in-one looping solution for loops that rely on incrementing or decrementing a variable. This is because it lets you initialize a loop variable, set the loop condition, and increment or decrement that variable all in one line of ActionScript. Although the for loop employs a different syntax than the while loop, everything you can accomplish with the while loop can also be done with the for loop. Personal preference will dictate which one you use. The syntax of the for loop is as follows:

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

The three elements within parentheses separated by semicolons 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 0. The script then goes on to state that as long as someNumber is less than 10, the loop will execute the actions contained therein. The last element specifies that someNumber will be incremented by 1 with each loop iteration, eventually causing someNumber to have a value of 10 (which means the loop will cease after nine iterations).

The for loop's structure means that it's used primarily to loop through a set of actions a specific number of times. Here is the same example given for the while loop using the for loop syntax:

 for (i=0; i<=10 ; ++i) {    myClip.duplicateMovieClip("myClip" + i, i);  } 

The for loop above duplicates myClip 10 times.

for . . in Loop

This loop which employs the following syntax is used to loop through all of an object's properties:

 for (i in _object) {    // perform these actions  } 

The i in the above loop is a variable that (with each loop iteration) temporarily stores the name of the property it references. The value of i can then be used in the actions within the loop. For a practical application of this, take a look at the following script:

 car = new Object();  car.color = "red";  car.make = "BMW";  car.doors = 2;  for (i in car) {    result = result + i + ": " + car[i] + newline;  } 

The first thing that happens in this script is that a generic object is created and given a name of car . The next three lines assign properties (which you can think of as simply variables within the car object) and corresponding values to the car object. Next, a for..in loop is set up to loop through all of that car object's properties, using i as a the variable that temporarily stores the name of each property. The value of i is then used in the action within the loop. When the loop is finished, result will contain a string of text that contains the name of each property as well as its value.

On the first iteration of the loop, i will have a string value of "doors" (since that was the name of the last property defined, as shown). Thus, during the first loop, the expression that sets the value of result would look like this:

 result = result + "doors" + ": " + 2 + newline; 

Thus, after the first loop, results will have a string value of:

 "doors: 2" 

In the expression that sets the value of result , the variable i (without brackets) refers to the property name (such as "doors", "make", or "color"). Using car[i] (that is, placing i between brackets) is the same as writing car.doors and thus is a reference to that property's value.

graphics/10fig03.gif

When complete, result will have a string value of:

 "doors: 2  make: BMW  color: red" 

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

You can use this type of loop for any of the following:

  • To find the name and value of every variable in a timeline or object

  • To find the name of every object in a timeline or object

  • To find the name and value of every attribute in an XML document

TIP

When you create a variable on an object, it's stored in what's known as an associative array. As you know, in a regular array, elements are referenced by number (starting with 0). In an associative array, in contrast, elements are referenced by name. The loop above loops through the associative array that contains all of these references in a specific timeline or object.




Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net