Learning Goals 3 min
By the end of this lesson you will be able to:
- Use create clone of [myself v] inside a forever loop to spawn copies of a sprite on a steady beat.
- Write a clone-only script under when I start as a clone that controls one falling kuih from spawn to delete.
- Pick a random horizontal spawn point with pick random (-220) to (220) so no two kuih fall down the same column.
Warm-Up — the kuih that only falls once 7 min
Aisyah from KL is making a catch-the-kuih game for her little brother. She drags in a kuih sprite, drops it at the top of the Stage, and writes the smallest possible "fall" script:
when flag clicked
go to x: (0) y: (170)
repeat (70)
change y by (-5)
end
She clicks the flag. The kuih slides down once. Then the Stage just sits there. Her brother yawns.
What's missing for a game?
A game needs many kuih, not one. And they need to come from different x positions so the player has to move the basket. Aisyah needs to (a) make a fresh kuih every second or so, and (b) pick a new random x each time. Today's lesson is the block that does both — clones.
Now look at this second sketch. Predict what happens before you read the reveal:
when flag clicked
forever
go to x: (0) y: (170)
repeat (70)
change y by (-5)
end
end
Reveal
One kuih teleports to the top, slides down, teleports back, slides down — over and over. You only ever see one kuih at a time. There is nothing to "catch many of". A single sprite can only be in one place at a time. That is exactly the problem clones solve.
New Concept — clones 15 min
A clone is a temporary copy of a sprite that runs its own scripts on its own. The original sprite stays put. The clone starts with the same costume, size and position — then does whatever its when I start as a clone hat tells it. When it is no longer needed, you call delete this clone and it vanishes cleanly.
Two hats, two jobs
A clone-based sprite almost always has two scripts working as a team:
- The spawner script sits under when flag clicked. It hides the original (we don't want to see the "mother kuih") then loops, making one clone every so often.
- The clone script sits under when I start as a clone. Every clone runs this the moment it is born — show itself, pick a position, fall, delete.
when flag clicked
hide
forever
wait (1) seconds
create clone of [myself v]
end
The 480×360 Stage and "off-edge" rules
The Stage goes from x: −240 on the far left to x: 240 on the far right. Top is y: 180, bottom is y: −180. We use −220 to 220 for x so kuih don't spawn half-off the edge, and y: 170 for the spawn height so they appear just on-screen.
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
end
delete this clone
Why repeat-until and not forever
A forever loop never ends. Put the falling code in a forever and the clone keeps "falling" off the bottom forever, while Scratch stacks thousands of invisible clones until everything freezes. A repeat until <> with the boolean () < () checking y position gives a clean exit — the moment the kuih's y drops below −170, the loop ends and we hit delete this clone.
Worked Example — Aisyah's kuih shower 12 min
Open Scratch. We will build the falling-kuih half of the game in eight steps. No basket yet — that is next lesson.
Step 1 — Start a fresh project
New Scratch project. Delete the default cat. Pick the Apple sprite from the library to stand in as a kuih (or paint a kuih-coloured ball). About 60×60 pixels works well.
Step 2 — The spawner hat
On the kuih, drag in when ⚑ clicked. Snap a hide right under it. The original kuih disappears the instant the flag is clicked.
Step 3 — The forever spawn loop
Add forever. Inside, add wait (1) seconds then create clone of [myself v]. Read it out loud: "Every one second, make a new copy of me."
Step 4 — Click the flag and see nothing
Click the flag. Nothing visible happens. Why? No clone has a script yet — they are born hidden, inheriting the original's hide. Invisible kuih are piling up in space.
Step 5 — Add the clone hat
Start a brand-new script beside the spawner. Drag in when I start as a clone. This hat only fires for clones, never for the original.
Step 6 — Random x, fixed y, show
Snap on go to x: () y: (). Into the x slot, drop pick random (-220) to (220). Type 170 in the y slot. Add show so the new clone becomes visible.
Step 7 — Fall until the bottom
Add repeat until <>. Into the diamond, drop () < () with y position on the left and -170 on the right. Inside, put change y by (-5). The kuih now slides down 5 pixels per frame.
Step 8 — Clean up
Below the repeat-until, snap on delete this clone. The moment a kuih falls below y: −170, it disappears. No invisible pile-up.
The full assembled stack
when flag clicked
hide
forever
wait (1) seconds
create clone of [myself v]
end
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
end
delete this clone
What you just built: the core of every falling-things game — Tetris, Galaga, every catch-the-X game ever. The spawner makes things appear on a beat. The clone script owns the life of one thing from birth to death. We add the basket in SCR-L02-38 and the scoring/game-over flow in SCR-L02-39.
Try It Yourself — three spawn tweaks 15 min
Goal: Make the kuih fall faster. Don't change the wait time — just change the falling speed inside the clone script.
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-10)
end
delete this clone
-5 became -10. The kuih now fall twice as fast.Think: The spawn rate didn't change, but each kuih is on-Stage for less time, so there are fewer kuih visible at once. Speed and density are linked.
Goal: Spawn kuih more often — one every 0.5 seconds — but only between x: -100 and x: 100 (a narrow middle band). It looks like rain through a slot.
when flag clicked
hide
forever
wait (0.5) seconds
create clone of [myself v]
end
when I start as a clone
go to x: (pick random (-100) to (100)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
end
delete this clone
1 → 0.5) and two in the random range.Think: We cover the spawn rate as its own difficulty knob in L02-40 — the game speeds up the longer you survive. For now, feel how the wait number controls density.
Goal: Random fall speed per kuih. Some drop slowly, some fast. Add a speed variable inside the clone script (set "For this sprite only" so each clone gets its own copy) and pick a random number from 3 to 9.
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
set [speed v] to (pick random (3) to (9))
show
repeat until <(y position) < (-170)>
change y by ((0) - (speed))
end
delete this clone
y change we need.Think: Per-clone variables are a Level 3 idea, but a stretch student is ready. Each clone remembers its own speed — the spawner doesn't have to care.
Mini-Challenge — Daniel's frozen kuih 5 min
"Daniel's kuih never come down"
Daniel built the falling-kuih sprite for his teh tarik shop game. He clicks the flag. One kuih appears at the top — and just sits there. No falling. The Stage fills with frozen kuih at the top, one per second. Here is his clone script:
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change x by (-5)
end
delete this clone
Spot the bug. Two things are wrong, and one means the repeat-until never finishes even if you fix the other.
Reveal the fix
Bug 1: The change inside the loop is change x by (-5), not change y by (-5). The kuih slides left, not down. It never changes its y, so the exit condition (y position) < (-170) never becomes true.
Bug 2: The loop body and the loop exit must talk about the same axis. The exit tests y but the body changes x — a mismatch. Always check the value you are actually changing.
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
end
delete this clone
Loop exit and loop body must talk about the same thing. If you change y inside the loop, check y in the exit. Mismatch = forever-stuck clone = laggy game.
Recap 3 min
You met clones — the way one sprite makes many copies that each live their own little life. A clone system always has a spawner (one forever loop calling create clone of [myself v] on a beat) and a clone script (under when I start as a clone) that owns one clone from birth to delete this clone. The original hides; the clones show. Each clone picks its own random x so the kuih spread across the Stage instead of stacking in one column.
- Clone
- A temporary copy of a sprite that runs its own scripts. Made with create clone of [myself v], removed with delete this clone.
- Spawner
- A script — usually a forever loop under when ⚑ clicked — whose only job is to keep producing clones on a steady beat.
- Clone script
- The script under when I start as a clone. Every new clone runs it from the top once. The original ignores it.
- pick random
- An Operators block that returns a different whole number each run, between the two numbers you give. Perfect for varying spawn positions.
- repeat until
- A control loop that runs its body until the boolean in its diamond becomes true. We use it to fall "until I'm off the bottom".
Homework 2 min
The Kuih Shower. Build the kuih sprite from this lesson on its own, with no basket. Hand it in as a stand-alone "rain" project. Tomorrow we add the basket.
- New project. Delete the cat. Add a kuih sprite (or paint one).
- Build the spawner script exactly as in Steps 2–3 of the worked example.
- Build the clone script exactly as in Steps 5–8.
- Click the flag. Kuih should spawn at random x positions and fall to the bottom, one per second.
- Now tweak: change the wait time to
0.3seconds and the fall speed to-3. Watch the Stage fill with slow kuih. Then change them back.
Save as HW-L2-37-Kuih-Shower.sb3.
Bring back next class:
- The
.sb3file with the original settings (1 second wait,-5fall speed). - Your answer to: "What happens if you delete the delete this clone block? Run it for 30 seconds and describe what you see."
- One sentence: "My kuih spawn at x: ___ to ___ and fall at ___ pixels per frame."
Heads up for next class: SCR-L02-38 · Catch & Reset adds the basket sprite at the bottom and teaches each kuih clone to ask touching (Basket v) ?. Bring this kuih-shower project — we add to it directly.