Skip to main content

Stage 6: Spawn the Swarm

Course progressStage 6 of 10
~90 min
Your workspace

Keep your Scratch project tab open all week. Open in a new tab so you don’t leave the course.

Build

a forever loop that spawns a new clone every few seconds

Learn

how one sprite becomes many through cloning

Ship

an enemy swarm — even if the clones don't move yet

Teacher demo

Show the room:

  1. Click the Fish sprite (the Enemy Fish). Click the Code tab.
  2. Drag in when green flag clicked (Events, yellow).
  3. Add set rotation style (left-right) so future clones flip cleanly instead of rotating freely.
  4. Add hide variable (enemySize) — we don't want the size shown on stage.
  5. Add hide — the template itself is invisible. Only clones appear.
  6. Add forever loop. Inside, add wait (pick random 1 to 2) seconds then create clone of (myself).
  7. Click the green flag. Wait. New fish appear at the template's spot — but they don't move yet. "That's tomorrow."

The "wait, where are they all stacking?" moment is the reaction we want.

The big idea

Yesterday we built the Enemy Fish sprite, but it just sits there. Today we make it multiply.

Scratch has a powerful trick called cloning. One sprite, the template, can spawn copies of itself called clones. The template stays hidden. Only the clones appear in the game. The template can run the spawn loop; each clone can run its own behavior — and that's the magic of cloning.

Fish sprite (template)

│ hidden, runs the spawn loop

forever:
wait 1-2 seconds
create clone of myself


Clone 1 Clone 2 Clone 3 Clone 4 ...
(each one is its own actor)

Today we just spawn the clones. They won't do anything yet — they'll all appear at the same spot and pile up. Stage 7 makes them come to life with random positions, sizes, and motion.

The random spawn interval (1–2 seconds) is a small detail that matters. A fixed-time spawn (exactly every 1 second) feels mechanical. A random range feels alive — like fish naturally appearing at slightly different beats.

New words
clone
a copy of a sprite that runs the same scripts but lives its own life
template
the original sprite that gets copied — usually hidden in the game
create clone of myself
the block that spawns a new copy of the sprite running it
pick random
an Operators block that picks a different number every time
hide
make the sprite invisible — but it's still in the game
Before you start

Stage 5 should be done — your Fish sprite has two costumes (fish-b, fish-c), Chomp sound, and three variables (enemySize, fishEaten, score).

Build it

Step 1 — Open the Fish sprite's code area

In the sprite pane (bottom-right), click the Fish sprite. (Not the Player — the new one we made in Stage 5.)

At the top of the editor, click the Code tab. The code area is empty.

Step 2 — Add the setup blocks

Every new game needs the Fish sprite ready: rotation style set, the size variable hidden, and the template itself hidden so it doesn't sit on stage.

Drag these blocks into the code area, snapping them together:

Enemy template setup

when green flag clicked
set rotation style [left-right v]
hide variable [enemySize v]
hide

Notes on each:

  • when green flag clicked — Events block, yellow. Start of the spawn script.
  • set rotation style (left-right) — same trick as the Player Fish: future clones flip cleanly instead of rotating freely when swimming left.
  • hide variable (enemySize) — keep the size readout off the stage.
  • hide — make the template Fish invisible. We don't want a static fish sitting on the screen.

Step 3 — Add the forever loop

The clones need to keep coming. We add a forever loop that spawns one every couple of seconds.

Drag a forever block from Control. Snap it under the hide block.

Step 4 — Wait, then clone

Inside the forever loop, add two blocks:

forever
wait (pick random (1) to (2)) seconds
create clone of (myself)

How to build the wait line:

  1. From Control, drag wait (1) seconds.
  2. From Operators (green), drag pick random (1) to (10).
  3. Drop the pick random block inside the wait's number slot.
  4. Change the second number from 10 to 2.

So the line reads: "wait a random amount of time between 1 and 2 seconds."

Then add create clone of (myself) from Control. The dropdown lets you choose what to clone — leave it on myself.

Your full script:

Enemy spawn loop

when green flag clicked
set rotation style [left-right v]
hide variable [enemySize v]
hide
forever
wait (pick random (1) to (2)) seconds
create clone of [myself v]
end

Step 5 — Test the spawn

Click the green flag. Wait a couple seconds.

You should start to see multiple Fish sprites appear on the stage. They'll all stack up at one position because we haven't told them where to go yet — that's Stage 7. For now, just confirm clones are spawning.

If you see something (even a pile) you got the spawn working.

Save your project.

Understand it

The most important pattern today is template + clones. The Fish sprite is the recipe; each clone is a fish that uses the recipe to start, then lives its own life. The recipe (the template) stays in the kitchen (hidden). The dishes (clones) go to the table (the stage).

The reason we hide the template is purely visual. Without hide, the template Fish would sit on screen as a permanent extra fish. The player would think it's an enemy, but it never moves and never gets eaten. Hiding it cleans up the stage.

The pick random 1 to 2 for the wait time is a tiny game-design move. A constant interval (always exactly 1 second) makes the spawn feel like a metronome. Slightly random (anywhere from 1 to 2 seconds) makes the spawn feel like nature. Variety in timing is what makes a swarm feel alive instead of robotic — same lesson as Stage 4 of AI Camp: designed unpredictability separates a game from a worksheet.

The forever loop is on the template, not on the clones. That's a critical separation. The template's job is to keep producing. The clones' job (Stage 7) is to live their own lives. If we put the spawn loop on each clone, each clone would spawn more clones — and Scratch would crash from too many fish in seconds.

We didn't add any clone behavior today. The clones spawn, sit at the template's position, and pile up. Stage 7 fixes this — when a clone is born, it runs its own setup script (random position, random size, random direction).

Try this

Learning beat

Try this

Three short experiments. Predict before you run, then test your guess.

Predict first

Change pick random (1) to (2) to pick random (0.1) to (0.2). Predict what happens to the stage. Try it. (Be careful — too many clones at once can crash Scratch.) Set it back to (1) to (2) when you're done.

Compare

Replace create clone of (myself) with create clone of (Player) using the dropdown. Click the green flag. What happens to the Player Fish? Why is myself the right choice for an enemy spawner?

Connect

Stage 7 will give each clone its own random position, size, and direction. Where will that code go — on the template Fish, or on each clone? (Hint: look at the when I start as a clone block in the Control category.)

Test your stage

  • Clicking the green flag hides the template Fish.
  • After a few seconds, new fish appear on the stage.
  • More fish keep appearing — every 1–2 seconds.
  • The clones all spawn in the same spot (that's expected for now).
  • Your project is saved.
  • Design check. Watch the stage for 30 seconds. Does the spawn rhythm feel alive (irregular, surprising) or mechanical (perfectly steady)? You can tune (1) to (2) to taste.

If it breaks

  • No fish appear at all. Three suspects. First, is create clone of (myself) inside the forever loop? Second, is myself selected in the dropdown? Third, did you click the green flag (not just the green flag on a single block — the big green flag at the top of the stage)?
  • The template Fish is still visible on stage. The hide block is missing or in the wrong place. It should come BEFORE the forever loop.
  • Clones spawn but the template's old static body is also on stage. Click the green flag again. The setup section should hide the template. If it still shows, try refreshing the project.
  • Scratch slows down or freezes. You probably have hundreds of clones — Scratch caps at 300 then stops creating. This is fine if you let (1) to (2) run for 10+ minutes. Click the red stop sign to reset.
  • The clone rotation looks weird. The set rotation style (left-right) block is missing from the setup section. Add it.
Coach notes

This stage is short for a 90-min slot. Plan a snack break around minute 30. The next stage (Stage 7's clone behavior) is the densest single script in the entire course — kids need to come into it focused.

The single most common failure: forgetting to hide the template. The template Fish sits on stage as a static fish, confusing the kid. Walk the room and check every laptop has the hide block in setup.

The second most common: setting (1) to (1) instead of (1) to (2) by accident. The spawn becomes a perfect metronome. The fix is to look at the random block's two slots and make sure they're different numbers.

For kids who finish in 30 minutes, push them straight into the medium stretch (spawn rate tuning). They'll have 60 minutes of legitimate exploration. The hard stretch (cloneCount cap) is real engineering — only push if they're way ahead and ready to write logic that spans two scripts.

Heads up for Stage 7: the clone-behavior script is dense (13+ blocks). Coaches should pre-build it on their own demo project and be ready to walk every camper through it twice if needed.