The WHIE Loop - 07WhileLoop.flaThe While Loop is used when you need to repeat an operation several times. Which kind of loop ???
This program creates a movie clip and scribbles random lines.
|
|
|
|
Set up your Flash MX environment like this ... |
|
Enter this code
on (release)
{
_root.createEmptyMovieClip("myScribble", 1);
with (_root.myScribble) {
lineStyle(2, 0xFF0000, 100); // 2 thickness, 0xFF0000 colour, 100% alpha
x1 = random(300);
y1 = random(150);
x2 = random(300);
y2 = random(150);
while (x1 > 1)
{
moveTo(x1, y1);
lineTo(x2, y2);
x1 = random(300);
y1 = random(150);
x2 = random(300);
y2 = random(150);
}
}
}
|
|
Here is the example movie. Zero or more randomly positioned lines will be scribbled.
You can download the source code. |
|
Tasks
|
|
NotesExample
Finished is a Boolean variable (a true or false value). Finished is initialised to false; The WHILE loop continues repeating as long as Finished is false. (! Finished) means Not Finished. The exclamation mark is the Boolean NOT operator. doThis() is a function that preforms some useful task and also returns a Boolean value. When doThis() returns a true value, the loop will stop repeating.
|
© C N Bauers (2004) - You may use these materials for your self education. Educational institutions may use these materials too.