- Learnt28 min · scroll x, screens as clones, the edge of the Stage
- Basic24 min · the road
- Challenge 1–327 min · the ends, parallax, the last screen
- Extrabonus · a map
Review — the Jump Quest arc 5 min
y velocitychanges by gravity; a jump sets it;landpushes out.walkundoes a move into a wall; a hitbox costume does the touching.- Water, a checkpoint and a star made a level worth finishing.
Quick-fire
In Jump Quest, the whole level had to fit inside the Stage. What would you have to do to make a level four times as long?
Reveal the answer
You cannot make the Stage bigger — it is always 480 × 360. So the level has to move instead. That is the whole of today: Pico stays, the world slides.
Today’s Topic 3 min
scroll x— how far along the world the Stage is lookingmy x— where a thing lives in the world, not on the Stage- A world made of four screen-sized clones
- Why a sprite far away must hide
Learning outcome
By the end of this lesson you will be able to:
- Make a world longer than the Stage by moving it instead of the player.
- Place anything in that world with
set x to ((my x) - (scroll x)). - Explain why a far-away sprite waits at the edge unless it hides.
Learnt — the world moves 28 min
1 · The Stage is only 480 wide
Walk Pico right the way you always have, and watch what happens at the edge.
How to use this: Click the green flag. Pico walks right with change x by 4 until it cannot.
2 · Move the world instead
Give the game one number, scroll x: how far along the world the Stage is looking. The arrow keys change that, not Pico. Everything in the world keeps its own place in a variable called my x, and works out where that is on the Stage:
when I receive [scroll v]
set x to ((my x) - (scroll x))
scroll x at 900, belongs at x 60 on the Stage. As scroll x grows, it slides left.The road is four screens, each a costume 480 wide. One sprite makes three clones and becomes the fourth screen itself, and each copy remembers its own my x — made for this sprite only, like the clones in Bubble Popper:
when flag clicked
set [screen v] to (0)
repeat (3)
change [screen v] by (1)
set [my x v] to ((480) * ((screen) - (1)))
switch costume to (join [road ] (screen))
create clone of (myself v)
end
change [screen v] by (1)
set [my x v] to ((480) * ((screen) - (1)))
switch costume to (join [road ] (screen))
scroll too, so it had better be a screen.How to use this: Click the green flag. scroll x climbs to 1440 on its own; watch the numbered signs go past Pico.
3 · Far things wait at the edge
Here is a surprise. The finish flag lives at 1600, so at the start it belongs at x 1600 — far off to the right. But Scratch will never let a sprite leave the Stage completely. It keeps a little of it showing, so the flag stands at the edge, waiting, from the first second.
How to use this: Click the green flag and look at the right-hand edge of the Stage before anything moves.
The cure: anything more than a screen away hides.
when I receive [scroll v]
set x to ((my x) - (scroll x))
if <([abs v] of ((my x) - (scroll x))) > (480)> then
hide
else
show
end
x position — Scratch has already pulled that back to the edge.Basic — the road 24 min
Start a new project. You are building a road four screens long and walking along it.
Step 1 — paint four screens
- Make a sprite called
Ground. Paint its first costume: a green road along the bottom, and a numbered signpost. - Duplicate it three times and change the number on each sign. Name the costumes
road 1toroad 4. - Keep the road the same height at both edges of every costume, so the screens join up.
Step 2 — two kinds of variable
scroll x, for all sprites.my xandscreen, on Ground, for this sprite only.
Step 3 — build the road
- Build the clone-making script from Learnt part 2.
- Add the
when I receive [scroll v]script from part 3, with its hide.
Step 4 — Pico, who does not walk
- Add Pico at size 40, standing on the road at x 0, in front of everything.
- In a forever loop: the right arrow changes
scroll xby 4, the left arrow by −4, and every tick ends withbroadcast [scroll v]. - Point Pico the way it is going, so it still looks as if it is walking.
when flag clicked
go to x: (0) y: (-104)
set [scroll x v] to (0)
forever
if <key (right arrow v) pressed?> then
point in direction (90)
change [scroll x v] by (4)
end
if <key (left arrow v) pressed?> then
point in direction (-90)
change [scroll x v] by (-4)
end
broadcast [scroll v]
end
How to use this: Click the green flag and hold the right arrow. Walk to sign 4 and the flag; walk back again.
All four screens sit on top of each other.
my x was made for all sprites, so every clone reads the same number. Delete it and make it again, for this sprite only.
The screens move the wrong way.
It is (my x) − (scroll x), not the other way round. As you walk right, things must slide left.
There is a gap or a step where two screens meet.
The road is a different height at the edges of two costumes. Line them up in the paint editor.
Pico is hidden behind the road.
Send the Ground clones to the back layer, or bring Pico to the front after they are made.
The world has ends
Walk left at the start and the road runs out. Stop scroll x going past either end.
- It never goes below 0 — the start of the road.
- It never goes above 1440 — where the fourth screen fills the Stage.
- Test both ends by holding the key down.
How to use this: Click the green flag. Forty presses left at the start, then right all the way to the end.
Teacher note
Ask why the end is 1440 and not 1920. Four screens start at 0, 480, 960 and 1440 — at 1440 the last one fills the Stage, and one pixel more would show the world running out. Working that out from the numbers, rather than trying it, is the skill.
Clouds far away
Add clouds that slide past slower than the road, as if they were much further away.
- A Cloud sprite with clones, each with its own
my x. - Clouds divide the scroll by 4:
((my x) - ((scroll x) / (4))). - They hide when they are well off the Stage.
How to use this: Click the green flag and watch the clouds against the signs.
Teacher note
This is called parallax, and it is the cheapest depth in games: things further away move more slowly across your view. Mountains at /8, clouds at /4, bushes at /2 — each a different number in the same block.
The last screen
At the end of the road the world cannot scroll any more, but the flag is still ahead. Let Pico walk the last stretch itself.
- If
scroll xis below 1440, the arrow scrolls the world. - Otherwise, the arrow moves Pico.
- Pico reaches the flag.
How to use this: Click the green flag. Pico starts near the end of the road and keeps going right.
Teacher reveal — one if / else
if <(scroll x) < (1440)> then
change [scroll x v] by (4)
else
change x by (4)
end
Walking back is the harder half: on the last screen, left must move Pico until it is back in the middle, and only then start scrolling. Next lesson solves both directions at once with a better idea.
A map
Put a marker along the top of the Stage that shows how far through the road Pico is: the left end at the start, the right end at the finish.
- The marker runs from x −150 to x 150.
- Its x is worked out from
scroll xevery tick — Vending Machine maths. - Halfway along the road, it is exactly halfway along the map.
How to use this: Click the green flag and watch the star along the top as the road goes by.
Teacher note
The map is the same idea as the world in reverse: the world turns a world position into a Stage position by subtracting; the map squeezes one down by dividing. Both are the sentence “where does this belong on the screen?”.
Summary 5 min
- The Stage cannot grow, so the world moves instead of the player.
scroll xis how far along the world you are;my xis where a thing lives in it.- Everything sets
set x to ((my x) - (scroll x))when told to scroll. - A world of four screens is one sprite and three clones.
- Scratch keeps sprites on the Stage, so anything far away must hide.
- Scrolling
- Moving the world past a player who stays near the middle.
- World position
- Where something is in the whole level, not on the Stage.
- Screen
- One Stage-sized piece of the world.
- Parallax
- Far things sliding past more slowly than near ones.
Hand in
Save your road. Next lesson Jump Quest’s Pico moves in — with gravity, jumps and walls — and it all still works.