T-W > while

while

Syntax

while(condition) {
statement(s);
}

Arguments

condition The statement that is reevaluated each time the while action is executed. If the statement evaluates to true, the expression in the statement(s) is run.

statement(s) The expression to run if the condition evaluates to true.

Description

Action; runs a statement or series of statements repeatedly in a loop as long as the condition argument is true. At the end of each while action, Flash restarts the loop by retesting the condition. If the condition is false or equal to 0, Flash skips to the first statement after the while action.

Looping is commonly used to perform an action while a counter variable is less than a specified value. At the end of each loop, the counter is incremented until the threshold value is reached, the condition is no longer true, and the loop ends.

Player

Flash 4 or later.

Example

This example duplicates five movie clips on the Stage, each with a randomly generated x and y position, xscale and yscale, and _alpha property to achieve a scattered effect. The variable foo is initialized with the value 0. The condition argument is set so that the while loop will run five times, or as long as the value of the variable foo is less than 5. Inside the while loop, a movie clip is duplicated and setProperty is used to adjust the various properties of the duplicated movie clip. The last statement of the loop increments foo so that when the value reaches 5, the condition argument evaluates to false, and the loop will not be executed.

on(release) {
	foo = 0;
	while(foo < 5) {
		duplicateMovieClip("/flower", "mc" + foo, foo);
		setProperty("mc" + foo, _x, random(275));
		setProperty("mc" + foo, _y, random(275));
		setProperty("mc" + foo, _alpha, random(275));
		setProperty("mc" + foo, _xscale, random(200));
		setProperty("mc" + foo, _yscale, random(200));
		foo = foo + 1; 
	}
}

See also

do... while
continue