Macromedia Flash MX 2004 ActionScript Programming Tutorial

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you will learn about:

  • While Loops
  • Do…While Loops
  • For Loops
  • For/In Loops
  • Automating processes with Keyframe Loops

Loops

A loop repeats a series of actions as long as a specified condition exists. It consists of three elements: the condition, the actions to be repeated, and a counter that increments (or decrements) each time the actions are repeated. In ActionScript, there are four types of loops:

  • while
  • do…while
  • for
  • for…in

It’s important to be aware that the stage isn’t updated each time a loop is executed, so these kinds of loops aren’t practical for displaying the repeating actions on the stage. To display repetitive actions on the stage, use the onEnterFrame() event handler or the setInterval() function. The onEnterFrame() event handler is described under Automating Processes with Keyframe Loops and the setInterval() function is described later in the tutorial, when you use it to call a function that updates the contents of the stage at 1-second intervals.

As with if statements, you can find loop statements in the Actions toolbox, in the Conditions/Loops subfolder of the Statements folder:

While Loops

A basic while loop evaluates a condition and, if it’s true, executes the specified actions. As long as the condition remains true, the actions will continue to be executed. Normally, this condition uses a counter (declared as a variable), with one of the actions incrementing the counter:

var i = 0;

while (i <= 100) {

_root.myMovie_mc._alpha = i;

i++;

}

Above, the variable i serves as a counter that begins at 0 and is incremented each time the loop is executed. When the value of i reaches 100 (<= 100), the loop stops. Each time the loop is executed, the _alpha property of the movie clip is set to the value of i.

As with switch() statements, you can use a break action to exit a loop:

i = 1;

startLocation = _root.myMovie_mc._x;

while (i <= 100) {

if (_root.myMovie_mc._x == 550) {

break;

}

_root.myMovie_mc._x = startLocation + i;

i++;

}

The code above moves the myMovie_mc movie clip to the right from its current location, but exits the loop if the x-coordinate of the movie clip reaches 550, the edge of a stage of that size.

The continue action stops the execution of actions in a loop,  restarting, rather than exiting, the loop. This action is used to exclude values from a loop:

i = 1;

startLocation = _root.myMovie_mc._x;

while (i <= 100) {

if (_root.myMovie_mc._x == 275) {

i++;

continue;

}

_root.myMovie_mc._x = startLocation + i;

i++;

}

Do…While Loops

In a do…while loop, the actions are executed once before the condition is evaluated. After the actions are executed, the condition is evaluated, and if it’s true, the actions are executed again, and continue to be executed as long as the condition evaluates to true:

i = 0;

do {

_root.myMovie_mc._x = i;

i++;

} while (i <= 550);

For Loops

The for loop offers you a means of combining the three actions of the while loop into one:

for (i = 0; i <= 550; i++) {

_root.myMovie_mc._x = i;

}

In the parentheses, the variable is assigned, the condition checked, and the counter incremented. Each of these is separated by semicolons.

For/In Loops

A for…in loop uses a placeholder variable referred to as a variableIterant rather than a condition statement. The variableIterant represents a property of an object or an index of an array. For every occurrence of the variableIterant, the actions are executed:

for (var myVarIt in _root) {

if (_root[myVarIt] instanceof MovieClip) {

_root[myVarIt]._xscale = 50;

_root[myVarIt]._yscale = 50;

}

}

Above, the variable myVarIt is assigned to represent all the movie clip instances in the main timeline. The code loops through every movie clip instance, changing the x and y scale values to 50%.

One Comment

  1. zoe

Leave a Reply

Your email address will not be published. Required fields are marked *