Learning Goals 3 min
By the end of this lesson you will be able to:
- Paint three backdrops on the Stage — Forest, Cave, Sky — and name them
level1,level2,level3so a script can choose them by number. - Use switch backdrop to (join [level] (level)) on the Stage so the backdrop auto-matches the value of the (level) variable.
- Use when backdrop switches to [level1 v] hats on each sprite to reset that sprite for the new level — player to start, coins re-spawned, enemies in position.
Warm-Up — three levels, one boring view 7 min
Last lesson you added a (level) variable that climbs from 1 to 3 every time the player reaches the goal. The number changes, but the game still looks the same forest from start to finish. Boring!
when flag clicked
set [level v] to (1)
forever
if <touching [Goal v] ?> then
change [level v] by (1)
broadcast (level-up v)
end
end
Predict — if you painted a second backdrop called cave and added switch backdrop to [cave v] right after change [level v] by (1), would it cope when level reaches 3?
Reveal the answer
Nope. You'd be stuck on the cave forever, because the script only knows the word "cave". To handle level 1, 2, 3, … n you need a script that picks the backdrop based on the level number, not by name. That's today's lesson.
Today: paint three backdrops, name them with the level number on the end, and let one tiny join trick pick the right one automatically.
New Concept — naming backdrops so scripts can find them 15 min
Scratch lets you switch a backdrop two ways: by dropdown (you pick a name) or by computed name (a script builds the name from text + a number). The second way is what makes a multi-level platformer possible.
Step A — paint three backdrops on the Stage
Click the Stage thumbnail (bottom-right, right of all the sprites), then the Backdrops tab at the top. Paint three:
- Forest — brown ground strip across the bottom, green trees behind.
- Cave — grey ground strip, dark walls, a few stalactites.
- Sky — white clouds, no ground (the player will jump on platform sprites).
Rename each one in the list on the left: level1, level2, level3. The names matter — that's how a script will find them.
Step B — the join trick
If (level) is 2, then join [level] (level) reports the text level2. Drop that into the Stage's backdrop block:
when I receive (level-up v)
switch backdrop to (join [level] (level))
level2. Works for any level number that has a matching backdrop name.Step C — sprites listen to the backdrop change
Now we need each sprite to react when the world changes. The Player needs to go back to the start. The Coin needs to re-appear. The Enemy needs to take its level-1 position again. Scratch gives every sprite a special hat for this:
when backdrop switches to [level1 v]
go to x: (-200) y: (-100)
level1, the Player snaps back to the left edge.Each sprite gets three such hats — one for level1, one for level2, one for level3. The hats only fire on a switch, so they're perfect for "set up this level".
Worked Example — wiring three Malaysian biomes 12 min
We'll build the spine of a three-biome platformer set somewhere in Malaysia: Taman Negara (forest), Gua Niah (cave), Genting clouds (sky). One Player, one Coin, one Enemy — and three backdrops.
x:−200, y:−100 via when backdrop switches to [level1 v]. Same hat pattern on every sprite, three times each.Step 1 — Paint three backdrops
On the Stage, Backdrops tab, paint Forest (brown ground), Cave (grey ground), Sky (white clouds). Rename to level1, level2, level3.
Step 2 — Make the level variable
Variables → Make a Variable → level, For all sprites. Tick the box so it shows on the Stage.
Step 3 — Stage script: start at level 1
when flag clicked
set [level v] to (1)
switch backdrop to [level1 v]
Step 4 — Stage script: auto-switch on level-up
when I receive (level-up v)
switch backdrop to (join [level] (level))
level-up after change [level v] by (1).Step 5 — Player: three start positions
when backdrop switches to [level1 v]
go to x: (-200) y: (-100)
when backdrop switches to [level2 v]
go to x: (-220) y: (-80)
when backdrop switches to [level3 v]
go to x: (-210) y: (-60)
Step 6 — Coin: re-show on every level change
when backdrop switches to [level1 v]
go to x: (150) y: (-60)
show
Step 7 — Enemy: position per level
when backdrop switches to [level1 v]
go to x: (50) y: (-100)
show
Step 8 — Test
Click the flag → forest, Player at x: −200. On the Stage, right-click the (level) watcher and pick change variable value, set to 2, then broadcast level-up from a temporary test stack. The backdrop should flip to cave and every sprite should jump to its level-2 position.
The Stage's full assembled stack
when flag clicked
set [level v] to (1)
switch backdrop to [level1 v]
when I receive (level-up v)
switch backdrop to (join [level] (level))
What you just built: a world that changes shape based on a single number. Add a fourth biome? Paint one more backdrop called level4 and add three when backdrop switches to [level4 v] hats. Zero changes to the Stage.
Try It Yourself — three biome drills 15 min
Goal: Add a fourth backdrop called level4 — paint a Sabah-beach scene with yellow sand. Wire just the Player so it starts at x: −230, y: −110 when level 4 begins. Don't touch the Stage scripts — the join trick handles it for free.
when backdrop switches to [level4 v]
go to x: (-230) y: (-110)
Think: You added one backdrop and one hat — zero edits to the Stage's level-switching script. That's why "name them with the number" is so powerful.
Goal: Play a short different sound on every backdrop switch. Forest = Crickets, Cave = Drip, Sky = Wind. Put the sound on the Stage so the Stage owns its own audio mood.
when backdrop switches to [level2 v]
play sound (Drip v) until done
Think: You could also use start sound instead of play sound until done. Start sound doesn't pause the script — the cave drip plays under the gameplay, not before it.
Goal: Make a small "Loading next level…" message appear for one second between backdrops. Put a sprite called LoadingBanner in the centre that shows on when I receive (level-up v), says Loading… for 1 sec, then hides.
when I receive (level-up v)
show
say [Loading next level…] for (1) seconds
hide
Think: The banner fires before the Stage finishes switching the backdrop (broadcasts run in parallel). For a polished version, use broadcast (level-up v) and wait from whatever sprite raised it.
Mini-Challenge — the cave that never shows up 5 min
"Nadia's stuck forest"
Nadia painted three backdrops — forest, cave, sky — and wrote the Stage script below. She clicks the flag, plays level 1, reaches the goal, and… nothing changes. The Player teleports back to x: −200 but the forest is still there.
when flag clicked
set [level v] to (1)
switch backdrop to [forest v]
when I receive (level-up v)
switch backdrop to (join [level] (level))
What's wrong, and what's the smallest fix?
Reveal one valid solution
Nadia named her backdrops forest, cave, sky — words. But the join script computes the name level2, level3, … Scratch can't find a backdrop called level2, so it doesn't switch. The script doesn't error — it just does nothing.
Two valid fixes. Pick one:
- Fix the names. Rename the backdrops to
level1,level2,level3. Update the flag-click to switch backdrop to [level1 v]. Done. - Fix the join. Build a list of names instead — item (level) of [biomes v] where
biomesis a list containingforest,cave,sky. (You met lists in L03-06.)
Lesson: when a script builds a name from text, the things it's looking for have to be named to match. Naming is a contract between two scripts.
Recap 3 min
You turned a single (level) number into three different worlds. The trick is naming — backdrops called level1, level2, level3 let the Stage build the right backdrop name with join [level] (level). Every sprite then listens with when backdrop switches to [level1 v] to set itself up for the new biome — start positions, visibility, spawn locations. Add a level? Paint one backdrop, add three hats. That's it.
- Backdrop
- An image on the Stage (not on a sprite). The Stage can have many backdrops, switched with switch backdrop to. Backdrops have names you can edit.
- Computed name
- A backdrop name that a script builds out of text plus a value — e.g. join [level] (level) reports
level1when level is 1. Lets one block handle any level number. - when backdrop switches to
- An events hat that fires the instant the Stage's backdrop changes to the chosen one. Used on every sprite that needs to reset, hide, or move when the world changes.
- Biome
- A themed environment in a game — forest, cave, sky, beach. In this lesson, one backdrop = one biome = one level.
- level-up broadcast
- A broadcast sent by whatever script increments the (level) variable. The Stage listens and switches the backdrop; sprites listen and re-position.
Homework 2 min
The Three-Biome Skeleton. Open a fresh project. Build a working level-switcher with no gameplay yet — just the world flipping.
- Paint three backdrops on the Stage:
level1(forest, brown ground),level2(cave, grey ground),level3(sky, white clouds). Optional fourth:level4for any Malaysian place you like. - Make a variable
level, For all sprites. Show its watcher on the Stage. - On the Stage: the two scripts from the worked example — flag-click sets level to 1 and shows level1; when I receive (level-up v) switches by join.
- On the Player sprite: one when backdrop switches to hat per level, moving the cat to a sensible start position for that biome.
- Make a tester button — a sprite called NextLevelButton with: when this sprite clicked → change [level v] by (1) → broadcast (level-up v). Click it three times in a row to walk through all three biomes.
Save as HW-L4-19-Three-Biomes.sb3. Test by clicking the NextLevelButton three times — backdrop should change forest → cave → sky and the cat should jump to a new start each time.
Bring back next class:
- The
.sb3file. - Your answer to: "What happens if you click the button a fourth time when there are only three backdrops? Try it and tell me what the Stage shows."
Heads up for next class: SCR-L04-20 turns that enemy sprite into three different enemies — slow patrols in the forest, fast jumpers in the cave, chasers in the sky — all on the same sprite, switched by the (level) variable.