Learning Goals 3 min
By the end of this lesson you will be able to:
- Spawn a fixed number of coin clones at the start of each level using repeat () + create clone of [myself v] triggered by the Stage's per-level broadcast.
- Give each clone its own pickup behaviour with touching (Player v)?, change [score v] by (10), and delete this clone.
- Track remaining coins in a coins-left counter and use the value to unlock the level exit only when all coins are collected.
Warm-Up — predict the empty level 7 min
Your three-backdrop game from SCR-L03-41 lets the player walk from level 1 → level 2 → level 3 just by reaching the right edge. There's no challenge — you can race straight through in ten seconds. Predict: what's the smallest gameplay element you could add to make each level feel like a thing the player accomplished?
when I receive [setup-level1 v]
go to x: (-200) y: (-100)
set [velocity-y v] to (0)
Reveal the answer
Coins. Or stars, or pisang, or anything collectible. Sprinkle a handful around each level. The player has to find and touch all of them before the exit unlocks. Suddenly each level has a goal beyond "walk right". This is the simplest gameplay loop in platformer history — Mario coins, Sonic rings, Pac-Man dots, Crash Bandicoot apples. We'll build it with one Coin sprite + clones + a coins-left counter.
Today combines almost everything from this cluster: clones from C, broadcasts from L03-41, lists for coin positions, and a counter variable for the unlock condition. Six small stacks; one feels-like-a-game project.
New Concept — one sprite, many coins, per-level spawn 15 min
You'll build one Coin sprite. The original sprite stays hidden the entire game. Every time a level loads, the Coin sprite hears the broadcast and spawns a handful of clones at the right positions for that level. Each clone is one coin; touch it and it vanishes. When the last clone is gone, the exit opens.
Step A — the original hides forever
The original Coin sprite never participates in gameplay. It exists only to spawn clones. So the first thing it does at flag-clicked is hide:
when flag clicked
hide
Step B — the per-level spawn
When the Stage broadcasts setup-level1, the Coin sprite reacts by deleting any leftover clones (in case the player retried) and spawning fresh ones. Each level can have a different number of coins:
when I receive [setup-level1 v]
set [coins-left v] to (5)
set [coin-index v] to (1)
repeat (5)
create clone of [myself v]
change [coin-index v] by (1)
end
Step C — the position lists
Lists are how we tell each clone where to spawn without hard-coding five separate go to blocks. Make two lists, both "for all sprites": coin-x-level1 and coin-y-level1. Type in five x-values and five matching y-values in the watcher. Add similar lists for level2 and level3 if those levels also have coins.
Step D — the clone reads its own slot
When a clone is born, it picks up the current value of coin-index and uses it to look up its position:
when I start as a clone
go to x: (item (coin-index) of [coin-x-level1 v]) y: (item (coin-index) of [coin-y-level1 v])
show
forever
if <touching (Player v) ?> then
change [score v] by (10)
change [coins-left v] by (-1)
delete this clone
end
end
Because the spawn loop changes coin-index after creating each clone, clone #1 catches it at 1, clone #2 catches it at 2, and so on. Five clones, five positions, one stack.
Step E — the unlock condition
The exit lives on the Player sprite's existing forever loop. The right-edge advance should only fire when coins-left hits zero:
if <<(x position) > (235)> and <(coins-left) = (0)>> then
change [level v] by (1)
switch backdrop to (join [level] (level))
end
Worked Example — five coins on level 1 12 min
Open PlatformerArc.sb3 from SCR-L03-41. We'll add coins to level 1 only — the same pattern extends to other levels in the homework.
Step 1 — Draw the Coin sprite
New sprite → Paint → draw a small yellow circle, about 20×20 pixels. Add a darker yellow ring around the edge so it stands out on any backdrop. Name it Coin. Centre the costume on the drawing canvas.
Step 2 — Hide the original
On the Coin sprite, add: when ⚑ clicked + hide. That's it for the original.
Step 3 — Make the variables and lists
Variables → Make a Variable → score, coins-left, coin-index. All "for all sprites". Variables → Make a List → coin-x-level1 and coin-y-level1. Both "for all sprites". Click the watcher for each list on the Stage and type in five x-values like -150, -50, 50, 100, 180 and five y-values like -80, 20, -50, 80, -100. (Adjust to land on your level 1's platforms.)
Step 4 — The spawn receiver
On the Coin sprite, drop when I receive [setup-level1 v]. Below it: set [coins-left v] to (5), set [coin-index v] to (1), then repeat (5). Inside the repeat: create clone of [myself v] and change [coin-index v] by (1).
Step 5 — The clone hat
Drop when I start as a clone. Below it: go to x: () y: () with item (coin-index) of [coin-x-level1 v] in x and the matching y-list reporter in y. Then show.
Step 6 — The pickup loop
Snap forever + if <> then with touching (Player v)? in the diamond. Inside: change [score v] by (10), change [coins-left v] by (-1), delete this clone.
Step 7 — Gate the level exit
Back on the Player sprite, find the right-edge check inside your forever loop. Wrap the condition: change if (x position) > (235) to if <(x position) > (235)> and <(coins-left) = (0)>.
Step 8 — Flag and play
Reset the project, click the flag. Five yellow coins should appear at the positions you typed into the lists. Walk and jump to touch each one — score should rise by 10 per coin, coins-left should drop, the coin should vanish. Walk to the right edge before collecting all five — nothing happens. Walk back, grab the last one, walk to the right edge — level 2 loads.
The full assembled Coin stacks
when flag clicked
hide
when I receive [setup-level1 v]
set [coins-left v] to (5)
set [coin-index v] to (1)
repeat (5)
create clone of [myself v]
change [coin-index v] by (1)
end
when I start as a clone
go to x: (item (coin-index) of [coin-x-level1 v]) y: (item (coin-index) of [coin-y-level1 v])
show
forever
if <touching (Player v) ?> then
change [score v] by (10)
change [coins-left v] by (-1)
delete this clone
end
end
What you just built: a complete collectible system. The same pattern works for stars in a maze, rubies in a dungeon, or buah for a kantin game. Swap the sprite costume and the colour and you have a different game.
Try It Yourself — three pickup drills 15 min
Goal: Add a pickup sound that plays the instant a coin is collected. Use the built-in Coin or Ding sound from the sound library. Add it inside the same if-then as delete this clone — but before the delete, otherwise the clone vanishes before the sound has a chance to start.
if <touching (Player v) ?> then
change [score v] by (10)
change [coins-left v] by (-1)
play sound [Coin v]
delete this clone
end
Think: Each clone has its own copy of "play sound", and they all use the same sound buffer. Touching three coins quickly will overlap three sounds — which actually feels good. That's free polish from cloning.
Goal: Make the coins spin while waiting to be collected. The Coin sprite should have one costume; add a tiny rotation to the clone's forever loop so each coin slowly spins. Bonus: also drift up and down using a sine-ish bob, but a simple change y by (...) based on a counter works too.
when I start as a clone
go to x: (item (coin-index) of [coin-x-level1 v]) y: (item (coin-index) of [coin-y-level1 v])
show
forever
turn cw (5) degrees
if <touching (Player v) ?> then
change [score v] by (10)
change [coins-left v] by (-1)
delete this clone
end
end
Think: Five degrees per frame, 60 frames per second — that's 300 degrees per second, or about one rotation per 1.2 seconds. Tune the number to taste. Each clone spins independently because each one has its own forever loop running its own turn block.
Goal: Make a special golden coin worth 50 points. Spawn one of them per level in addition to the normal coins. Use the same Coin sprite with a second costume named gold. The first clone of each level should switch to the gold costume and grant 50; the others stay normal.
when I start as a clone
go to x: (item (coin-index) of [coin-x-level1 v]) y: (item (coin-index) of [coin-y-level1 v])
if <(coin-index) = (1)> then
switch costume to [gold v]
set [my-value v] to (50)
else
switch costume to [normal v]
set [my-value v] to (10)
end
show
forever
if <touching (Player v) ?> then
change [score v] by (my-value)
change [coins-left v] by (-1)
delete this clone
end
end
Think: "For this sprite only" variables are how clones store per-clone data without colliding. If you'd used "for all sprites", the gold coin would set my-value to 50 and then the next clone's spawn would overwrite it to 10 — all coins worth 10. The "for this sprite only" tickbox is the difference.
Mini-Challenge — Khairul's stacked coins 5 min
"All five coins are sitting on top of each other"
Khairul wrote his coin spawn and clone hat. When he hits the flag, only one coin appears to be visible — but the counter shows 5 coins-left, and touching that one coin only decrements by 1 at a time. Five touches in the same spot to clear the level. Here are his stacks:
when I receive [setup-level1 v]
set [coins-left v] to (5)
set [coin-index v] to (1)
repeat (5)
create clone of [myself v]
end
when I start as a clone
go to x: (item (coin-index) of [coin-x-level1 v]) y: (item (coin-index) of [coin-y-level1 v])
show
forever
if <touching (Player v) ?> then
change [score v] by (10)
change [coins-left v] by (-1)
delete this clone
end
end
Trace it on paper. What is coin-index when each clone reads it?
Reveal one valid solution
Khairul forgot to bump coin-index inside the repeat. So every clone is born with coin-index = 1, every clone reads slot 1 of the x and y lists, and all five clones stack at the same (x, y). Five sprites, one visible position, total chaos.
The fix is one extra block inside the spawn loop, right after the create-clone:
when I receive [setup-level1 v]
set [coins-left v] to (5)
set [coin-index v] to (1)
repeat (5)
create clone of [myself v]
change [coin-index v] by (1)
end
Now clone #1 catches coin-index = 1, clone #2 catches 2 (because the original bumped it before the next loop iteration), and so on. Five clones, five slots, five visible coins. When clones share a counter via the original, the order matters: create the clone, then bump the counter for the next one.
Recap 3 min
You wired three previous Level 3 tools — clones, lists, broadcasts — into a complete pickup system. The Coin sprite hides itself; its only job is to spawn clones in response to per-level broadcasts. Each clone reads its own position from a shared list, updates score and coins-left on touch, and self-deletes. The level exit is now soft-locked behind coins-left = 0.
- Spawner
- The stack on the original sprite that creates clones. Lives under a hat like when ⚑ clicked or when I receive (); the spawning sprite itself stays hidden.
- Counter variable
- A variable like coin-index bumped inside a spawn loop so each clone catches a different value. The mechanism by which one stack assigns each clone its own slot.
- Per-clone identity
- The unique data (position, score-value, costume) that distinguishes one clone from another. Stored either via a "for this sprite only" variable or read at birth from a list.
- Position list
- A list whose entries are the x or y coordinates for each spawn. item (n) of [coin-x-level1 v] returns the nth coin's x.
- Soft-lock
- A condition that prevents progression until satisfied. The level exit here is soft-locked behind coins-left = 0 — visible but inactive until you've earned it.
- Atomic state update
- Score, coins-left, and delete-this-clone all happening inside the same if-then. They run together with no chance for the player to "double-collect" the same coin.
Homework 2 min
The Three-Level Coin Run. Extend the project so all three levels have their own coins.
- Make matching list pairs for the other two levels:
coin-x-level2/coin-y-level2andcoin-x-level3/coin-y-level3. Three coins for level 2, seven for level 3 (or whatever your level layouts can hold). - Add two more setup-receivers on the Coin sprite: when I receive [setup-level2 v] and when I receive [setup-level3 v]. Each one resets coins-left and coin-index for its own count, then repeats the spawn the right number of times.
- Update the clone hat so it knows which list to read from. The cleanest fix is one new variable, level-list-name, that each setup-receiver sets to
coin-x-level1,coin-x-level2, etc. — then the clone uses join in the item-of block to pick the right list. (If you find this fiddly, just duplicate the clone hat three times — one per level — and gate each on level = 1, etc.) - Confirm that dying mid-level respawns all the coins in their original positions, not just the ones you hadn't collected. (The setup-receiver fires fresh on every backdrop reload.)
- Display the score watcher on the Stage in large mode so you can show off the total at the end.
Save as HW-L3-42-Three-Level-Coin-Run.sb3.
Bring back next class:
- The
.sb3file. - Your final score after clearing all three levels (sum of coins × 10 each).
- Your answer to: "What happens if you forget to broadcast setup-level2 v when level 2 loads? Test it by commenting out the broadcast on the Stage and describe what's missing."
Heads up for next class: SCR-L03-43 stitches the player engine, the level system, and the coin system into a polished mini-platformer with enemies, a death animation, and a victory screen. It's the showcase project for cluster G.