Lingo 4.1: The "repeat" control structure: Use with care!

READING: Manual pages of the commands described here

Lingo, like every Programming Language includes structures that allow you to loop through a piece of code, such as the repeat loop. However, it should be used with care, as it may not even needed in many cases. For example, the repeat could be useful for performing calculations, searching through the items in a list, etc, but it is not useful (or safe) to use it for creating animation by replacing cast members in a sprite.

Below are two implementations of a "Dancing Arthur". The right way to do is using the "slower dance" which takes advantage of the continuous play of a Director movie for the animation (instead of the "fast dance" which relies on a repeat loop.)


File "DancingArthur.dir"

Shows how animation can be performed in a single frame (marker "fastdance") by rotating between cast members appearing in the channel.

on mouseUp
repeat with counter = 2 to 150
sampleImage = (counter mod 8) + 3 -- chooses between 3 and 10
put sampleImage
sprite(2).member = sampleImage
updateStage
end repeat
end


Guess what the command sprite(2).member does.

Why does the sampleImage rotate between 3 and 10?

Watch the animation carefully - do you really see 150 images of Arthur?
(Remember, Director will do its best to display the animation, but will try very hard to keep up with the timing requirements. If it cannot, it will ignore the animation. Timing uber alles!)

The correct way to animate: Through frame movement.

Play the file under the marker "slowerdance"
How does it work?
Observe that the core of the script is the same as above, but there is no repeat command. The exitFrame script provides the timing. Now you can control the animation speed using the Tempo channel.

Both animations use a counter. Is this the very same counter? Why not?

When is a global variable initialized? When you start a movie or every time you re-start a movie?

Note that you can make the animations more funny if you randomize the outcome of the mod calculation before assigning it to sampleImage

Also notice that you can make Arthur double-dance!


 

 

Maintained By: Takis Metaxas
Last Modified: October 22, 2012