The WHIE Loop - 07WhileLoop.fla

The While Loop is used when you need to repeat an operation several times.

Which kind of loop ???

  • If you know in advance how many repetitions you need - use FOR
  • If you need to repeat zero or more times - use WHILE
  • If you need to repeat one or more times - use DO

This program creates a movie clip and scribbles random lines.

  • Input - Button press to trigger the processing
  • Process - Create a movie clip and generate random numbers.
  • Output -
        Scribbled lines
  1. Start up Flash.
  2. Create a new Flash document.
  3. Save the file as 07WhileLoop.fla.
  4. Place a button onto your canvas.

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

  1. Add another button.
  2. Scribble lines in a different colour when this new button is pressed.
  3. Make the lines less random. For example make all the lines radiate from a single point.

Notes

Example

Finished = false;

while (! Finished)
{
  Finished = doThis();
}

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.