Loops

Loops

Flash CS4 ActionScript 3.0 Tutorial

 

How to Assign and Check Data with Flash ActionScript

 

 

Loops are handy pieces of code if you want to fill an array, add several Loaders or check several pieces of data. Once a loop starts, it consumes the processor resources. It is important that you don't create an endless loop that you can't escape.

Loops come in multiple varieties. Common loops include For, Do While and While statements.

In this example we will demo a For statement that loads an Array. For more information on the loops, see topics, Do While Statement, For Statement and While Statement.

For more information on Arrays, see topics, Accessing Array Data, Loading an Array and Manipulating Array Contents.

 

Step One

Open a new Flash document and name the default layer Actions.

Step Two

Open the ActionScript panel for the only keyframe in the Actions layer by Option double-clicking (PC - Alt double-clicking) the keyframe.

Step Three

Copy/Paste the following code in the ActionScript Panel:

var memberNumber:Array=new Array();

var startNumber:int=100;

for (var i:int = 1; i<11; i+=1) {

memberNumber[i]=startNumber;

startNumber+=1;

trace(memberNumber[i]);

}

Step Four

Click the Auto Format button to format the code.

Step Five

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The Output panel displays the Array values that you loaded with your for statement.

Dissecting the Code

The first two lines of the code at Step Three establish two new variables. The first is an Array called memberNumber and the second is an integer called startNumber. The initial value of startNumber is set to 100.

The for statement introduces a new variable named i and sets the starting value to one. The loop will run 10 times because it checks to make sure that i is less than 11. For each pass through the loop, i increments by one.

Inside the loop, memberNumber[i] is set to the current value of the variable startNumber. The startNumber +=1; line increments startNumber by one for each pass through the loop.

The trace statement displays the memberNumber value in the Output panel.

 

DAZ Studio FREE 3D MODELS AND SOFTWARE