Creating a Loop

Review

Programmers use a for loop to iterate (or repeat) statements a pre-determined number of times. The for loop contains a variable (usually called i for "iterate") that can be used to set unique properties for any objects contained inside the for loop braces.

We also determined there is more work to do.... we can only target one instance of our ball!

What We Did

  1. Created a new file called ball_for
  2. Used a for loop to add five copies of ball to the stage
  3. Used the variable i to set the X position of each instance of ball

Important ActionScript

for(var i:Number = 0; i<5; i++){
   var myBall:ball = new ball();
   myBall.x=100+i*50;
   myBall.y=150;
   addChild(myBall);
}

Self-Check for Understanding

In the following code, how many copies of enemy will be added to the Stage:

The answer -->10<--

And look at what's happening with the X position.... where would that be?