Do While Statement

Do While Statement

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. In other words, loops work great to populate an array, movie clip or the stage. 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. Note that For statements and Do While statements accomplish the same task. The Do While statement makes the test at the end of the loop and the For statement test is at the beginning of the loop.

In this example we will demo a Do While statement that loads several shapes to the stage. For more information on other types of loops, see topics, For Statement and While Statement.

 

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 boxCount:int=1;

do {

var cube01:Shape=new Shape;

cube01.graphics.beginFill(0xCCCCCC);

cube01.graphics.lineStyle(2,0xAAAAAA);

cube01.graphics.drawRoundRect(10,15,50,50,4);

cube01.graphics.endFill();

addChild(cube01);

cube01.x= boxCount*80;

boxCount+=1;

}

while(boxCount<6);

Step Four

Click the Auto Format button to format the code

Step Five

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). Five rounded rectangles appear on the stage.

Dissecting the Code

The first line of the code at Step Three establishes a new variable named boxCount and sets it's value to one.

The do statement specifies all the things that need to be done. A new Shape variable is introduced named cube01. Lines four through seven define the rounded rectangle shape.

The addChild() statement adds the object to the stage and the next line distributes the multiple objects across the stage.

The final line in the curly brackets increments the boxCount variable by one for each pass through the statement.

The final line is a while statement. As long as the boxCount variable stays below six, the Do portion of the statement continues to run.

 

Stock Photos from 123RF