GoalReview if … then in Scratch: plan a sprite, predict what a script does, find bugs and start every sprite from the same place.
New words
selection · a decision in a program, made with an if blockcondition · a question that is either true or falseconclusion · the blocks that run when the condition is truestart script · a script that puts a sprite back in its starting state
Explanation
Selection means that a program makes a decision. An if block holds a condition, and its conclusion is carried out only when the condition is true. If the condition is false, the program skips the conclusion. Inside a forever block the condition is checked again on every pass. To predict a script, work through it one pass at a time and write down each change. Before we build, we plan each sprite with four questions: its purpose, its input, its condition and its conclusion. Every sprite needs a start script, so that each run begins from the same place. When a program has two sprites, each has its own scripts, and a condition can use the other sprite.
Plan question
Answer for a Star sprite
Purpose
to be collected by the Rocket
Input
the green flag is clicked
Condition
the Star is touching the Rocket
Conclusion
the Star hides
Worked examples
The Rocket starts at y: -140 and moves up 20 whenever the up arrow key is held. On passes 1 to 4 the key is held and on pass 5 it is let go. The Rocket goes up 4 × 20 = 80, so it ends at y: -140 + 80 = -60.
The Star has this script. While the Rocket is far away the Star stays visible, because the condition is false. When the Rocket touches it, the Star hides. When the Rocket moves away again the Star stays hidden, because nothing inside the loop shows it again.
Bug
What you see
Fix
the if block is outside the forever block
it is checked only once
move the if block inside the loop
the start script has no show block
the sprite stays hidden on the second run
add show to the start script
the wrong sprite is selected
the script has no effect
drag the script to the right sprite
These are common bugs. Each one has a clue and a fix.
Class activity
Sprite plan cards (pairs). Draw a 3 × 3 grid on paper as the stage. One player is the Rocket and moves one square when the other player says ‘key pressed’. The second player is the Star, who stays on a square and says ‘hide’ if the Rocket is on the same square. Write a plan card first with purpose, input, condition and conclusion. Play, then reset both pieces to their start squares. Why is the reset needed before a second game?
Practice
1
The Bee is at x: 0, y: 0. The right arrow key is held on 6 of 8 passes. Where is the Bee after 8 passes?
The Bee moves 6 × 15 = 90 to the right, so it is at x: 90, y: 0.
2
Plan a Door sprite that changes its costume when the Child sprite touches it. Give the purpose, input, condition and conclusion.
Purpose: to open when a child comes. Input: the green flag is clicked. Condition: the Door is touching the Child. Conclusion: the Door switches to the open costume.
3
The Crab touches the Shell after 3 seconds but never says Munch. Why? How can it be fixed?
The if block is outside the forever block, so the condition is checked only once, at the start. Put the if block inside the forever block.
4
A sprite hides when it is touched. On the second run it is still hidden at the start. Which block is missing, and from which script?
A show block is missing from the start script (the script that begins with when green flag clicked).