4.7 Spawning with Loops
Spawning Waves of Actors with Loops in Greenfoot
Unit 4: Loops and Collections of Actors · Lesson 4.7 · about 30 minutes · no coding experience needed
Placing twenty enemies by hand is twenty lines you have to edit every time you change your mind. One loop does it, scales to any number, and this lesson is where every idea in the unit comes together.
What you will be able to do
- Spawn a row of actors with a single loop
- Space them out using the loop counter
- Explain why the spawn loop must not live in act()
Words you will need
- Wave
- In plain words A group of enemies spawned together.
- More precisely A batch of actors created in one call.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A row of five enemies spawned across the top of the world.
public void startWave() { for (int i = 0; i < 5; i++) { addObject(new Enemy(2), i, 0); } }Five enemies, one loop. Everything here you already have: the for loop from 4.2, addObject from 3.7, and the Enemy constructor from 3.9.
They land at x values 0, 1, 2, 3 and 4, all bunched at the left, which the next step fixes.
-
Step 2
On your screen The same five enemies spaced evenly across the world.
for (int i = 0; i < 5; i++) { addObject(new Enemy(2), i * 3, 0); }One change: `i * 3` instead of `i`. Now they sit at 0, 3, 6, 9 and 12.
Using the counter in a CALCULATION rather than directly is the single most useful loop trick in this course, and it is the whole basis of tile maps in Unit 6.
-
Step 3
On your screen A world flooded with hundreds of enemies after the spawn loop was put in act.
// DISASTER. act() runs every frame. public void act() { for (int i = 0; i < 5; i++) { addObject(new Enemy(2), i * 3, 0); } }This does not freeze. The loop finishes perfectly every time, so 4.1's rule is not broken.
It spawns five enemies EVERY FRAME. Within a second there are hundreds, and the scenario grinds to a halt for a completely different reason: not an infinite loop, but a finite loop run infinitely often.
Spawn loops belong in a method you call ONCE, from prepare() or when a wave begins.
-
Step 4
On your screen A new wave beginning automatically when the previous one is cleared.
public void act() { if (getObjects(Enemy.class).size() == 0) { waveNumber = waveNumber + 1; startWave(); } }And the whole unit assembled into a working game loop.
Every frame, the world checks whether the list of enemies is empty (4.5). When it is, the wave counter goes up (3.8, an instance variable) and the spawn method is called (4.7).
The loop is still called once per wave, not once per frame, because the if guards it. That is the difference between this and the previous step.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea A spawn loop in act() is fine because the loop finishes.
What is actually true It finishes, and then runs again next frame. Five per frame is hundreds a second.
Why it matters The Unit 1 rule was about freezing; this is a different failure with the same symptom of an unusable scenario.
Common wrong idea You must place actors one at a time to control where they go.
What is actually true A calculation on the counter gives you full control.
Why it matters This is the idea Unit 6 is built on, so it matters more than it looks.
Common wrong idea Spawning more enemies means writing more addObject lines.
What is actually true Change one number in the loop condition.
Why it matters The entire argument for loops, made concrete.
Common wrong idea The wave check belongs in an enemy.
What is actually true It belongs in the world, which is what knows about the game as a whole.
Why it matters An enemy checking whether all enemies are gone has to survive its own death to do it.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. Where does `addObject` go to spawn a row of five?
-
2. What does `i * 4` do as the x coordinate?
-
3. Why is a spawn loop in act() a problem, if the loop finishes?
-
4. How would you change a wave from 5 enemies to 12?
-
5. Where should the check for "all enemies defeated" live?
Fill in the code
Spawn a wave of eight enemies along the top, spaced two cells apart. This method is called once when a wave begins, not from act().
public void startWave()
{
for (int i = 0; i < ; i++)
{
addObject(new Enemy(2), i 2, );
}
}
Stuck? Open a hint for each blank
- Blank 1: How many enemies in the wave?
- Blank 2: Turn 0, 1, 2 into 0, 2, 4. Which operator?
- Blank 3: The top row of the world.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. A loop spawning 5 enemies is placed in the world act(). What happens?
-
2. Which places actors at x values 0, 5, 10 and 15?
-
3. A spawn method should be called from:
-
4. Using the loop counter in a calculation matters because:
The short version
- One loop spawns a whole wave; change one number to change the size.
- Use the counter in a CALCULATION to control spacing.
- A spawn loop in act() spawns every frame. Call it once instead.
- Wave logic belongs in the world, which knows about the game as a whole.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]