Using Director > Writing Scripts with Lingo > Controlling flow in scripts > Repeating an action |  |

Repeating an action

Lingo can repeat an action a specified number of times or while a specific condition exists.

To repeat an action a specified number of times:

Use a `repeat with` structure. Specify the number of times to repeat as a range following `repeat with`.

This structure is useful for performing the same operation on a series of objects. For example, the following repeat loop makes Background Transparent the ink for sprites 2 through 10:

    repeat with n = 2 to 10
    	set the ink of sprite n = 36
    end repeat

This example performs exactly the same action as above, but uses dot syntax:

    repeat with n =2 to 10
    	sprite(n).ink = 36
    end repeat

To repeat a set of instructions as long as a specific condition exists:

Use a `repeat...while` statement.

For example, these statements instruct a movie to beep continuously whenever the mouse button is being pressed:

    repeat while the mouseDown
    	beep
    end repeat

Lingo continues to loop through the statements inside the repeat loop until the condition is no longer true or until one of the instructions sends Lingo outside the loop. In the previous example, Lingo exits the repeat loop when the mouse button is released because the `mouseDown` condition is no longer true.

To exit a repeat loop:

Use the `exit repeat` command.

For example, the following statements make a movie beep while the mouse button is pressed, unless the mouse pointer is over sprite 1. If the pointer is over sprite 1, Lingo exits the repeat loop and stops beeping. (The term `rollover` followed by a sprite number indicates that the pointer is over the specified sprite.)

    repeat while the stillDown
    	beep
    	if rollover (1) then exit repeat
    end repeat

See `repeat with`, `repeat while`, and `exit repeat`.