ADDING IN FOR LOOPS

Consider something called a nested ‘For’ loop.

You often see these when it comes to any loop. To make this work, I will copy this object type and change the color.

We made it red, and that was perfect!

We can call it ‘Sprite1’

Let me show you how it works.

I’m going to add a sub-condition here, and we’re going to add one more time to the ‘For’ loop, and that ‘For’ loop will convert to ‘SpawnRed’. We set ‘End Index’ as ‘AmountOfSpritesToSpawn’

It will repeat in this case ten times.

It will repeat for a certain amount of time. I’ll spread it to destroy, then I can copy and paste it here and add ‘Sprite’ in.

Let’s change this to three and see what happens.

I run it here.

You see, we have one, two.

Then we have one, two, three, and four.

I click on this and have the subtracted one.

Let’s get rid of that code.

It is what I was looking for. Then if we count the red, it’s one, two, three, four, five, six, seven, eight, and nine.

Why? Remember that this code is looping if we were to expand it. I will copy and paste it three or four times.

We copy-paste it three times meaning for each time this occurs, it also happens three times.

It would be one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, and fifteen. It’s not exactly three times, but it repeats more times. As you can see, things get looped over and over again.

That’s what a nested ‘For’ loop can do. I keep this here.

In the next section, we’ll look at something more interesting.