The For Loop

Return to the main stage and bring up the Frame Actions for the first keyframe. Start a new function called init.

  1. Click Statements > User-Defined Functions > function
  2. Type the name init

We want to get 30 copies of the main Movie Clip on the stage. Each clip will have a unique instance name and depth. Basically, we need to run the attachMovie function 30 times! This sounds like too much coding, but remember that wondeful bit of programming magic: the for loop.

You're really going to love the this loop. Let's just get it started and I will explain how it works right after:

  1. Click Statements > Conditions/Loops > for
  2. Type n=0 in the Init box
  3. Type n<30 in the Condition box
  4. Type n++ in the Next box

Ok let's look at the three things happening with the for loop:

First, we are initializing a variable called n and setting it equal to zero: n=0. This is exactly like using set variable.

Second, we are checking to see if the variable n is less than 30: n<30. If n is less than 30, Flash will run all of the script inside the braces (when we put some script there). If n is not less than 30, the for loop is finished.

Third, we are incrementing the variable n by 1: n++. Then the script loops back to step two, where it checks to see if n is still less than 30.

Now let's put some Actions inside those braces!

We are going to attach that main MovieClip to the stage, so bring up the attachMovie action:

  1. The Object is _root because we want these new clips to go right on the main stage
  2. The Linkage Name is main and don't forget to deselect the Expression box
  3. The New Name is "main"+n which will create Movie Clips with Instance Names of main0, main1, main2.... all the way to main29. It's like magic, because it's math, which is the most beautiful magic ever.
  4. The Depth is n because every Movie Clip needs to have a unique depth. Remember, after every time the for loop executes, n = n+1; so we get depths of 0, 1, 2... all the way to 29.

OK, test it! If everything went as planned you won't get any Output errors, but you will get 30 copies of the main MovieClip sitting at the 0,0 position on the Stage (The top left corner). Actually, you probably won't even see them, so go to the next page for more instructions.