Music Sprite Programming
Senior 1 · ICT
Music Sprite Programming
Lesson 2 of 5
22 min
Press the play button on a music app and the music starts; press the same button again and it stops. The app must remember whether the music is on or off, so that it knows what the next press should do. A light switch does the same with two positions. In programs, this memory is kept in a variable. In this lesson you will use a variable as an on/off switch for music.
A variable is a named place in the computer's memory that holds one value, and the value can change while the program runs. Think of a box with a label on it: the label is the variable's name, and whatever is in the box is its value.
To create one in Scratch: open the orange Variables category, click Make a Variable, type a name such as switch, and choose For all sprites (every sprite can read and change it) or For this sprite only. Click OK. While its tick box is ticked, a small display called a monitor shows the value on the stage, which is very useful for testing.
In this unit, scripts are written as text, one block per line. Lines indented under a block sit inside it, and end marks where a C-shaped block closes. Round brackets ( ) hold a value or a choice from a menu, square brackets [ ] hold a name or a key, and pointed brackets < > hold a yes-or-no condition.
set [switch] to (1) replaces the value with 1, whatever it was before.change [switch] by (1) adds 1 to the value. change [switch] by (-1) subtracts 1.Worked example
tracing. The variable switch starts at 0 and these blocks run in order:
| Block | Value of switch after it |
|---|---|
| change [switch] by (1) | 0 + 1 = 1 |
| change [switch] by (1) | 1 + 1 = 2 |
| set [switch] to (0) | 0 |
| change [switch] by (-1) | 0 − 1 = −1 |
A switch (or flag) variable holds only two values. In this unit we use 0 for off and 1 for on. You could also use the words off and on; what matters is that every script uses the same two values.
| Block | What it does |
|---|---|
| forever | Repeats the blocks inside it again and again until the red stop sign is clicked |
| if < > then | Runs the blocks inside it only when the condition is true |
| if < > then … else | Runs the first group when the condition is true and the second group when it is false |
| wait (1) seconds | Pauses the script |
What about "forever if"? Old versions of Scratch (1.4) had a single forever if block that kept checking a condition and ran its blocks whenever the condition was true. Scratch 2 and 3 removed it. You build exactly the same thing by putting an if block inside a forever block.
The project has two sprites. A Button sprite switches the music on and off; a Speaker sprite plays the music. The Speaker has a sound renamed my tune. First, the Button:
Each click toggles the switch: if it is off (0) the click turns it on (1), otherwise the click turns it off. Clicking 5 times from 0 gives 1, 0, 1, 0, 1, so after an odd number of clicks the music is on.
This is another way to toggle, using change and set. From 0, one click makes 1. The next click makes 2, which is greater than 1, so it is set back to 0. The switch goes 1, 0, 1, 0 … exactly as before.
Now the Speaker. This is the "forever if" pattern:
The set block makes every run start with the music off. The forever loop keeps checking the switch many times a second. Whenever the switch is 1, the tune plays to the end and the loop checks again, so it keeps playing until someone clicks the button. When the switch is 0, the if skips the sound and nothing plays.
Tip
Set replaces, change adds. For "forever if" remember the order: forever on the outside keeps asking, if on the inside decides. An if that is not inside a loop checks only once.
The variable volume level is 5. What is it after change [volume level] by (3), then set [volume level] to (2), then change [volume level] by (-1)?
Solution: 5 + 3 = 8; then set to 2; then 2 − 1 = 1.
Using the first Button script, the switch starts at 0 and the button is clicked 6 times. Is the music on or off?
Solution: The values go 1, 0, 1, 0, 1, 0. After 6 clicks (an even number) the switch is 0: the music is off.
Habimana writes the Speaker script without the forever block: when green flag clicked, then if <(switch) = (1)> then play sound (my tune) until done. He clicks the green flag, then clicks the button. Why does no music play?
Solution: Without forever, the if checks the switch only once, straight after the green flag, when the switch is still 0. When the button later sets it to 1, nothing checks again. Put the if inside a forever block.
Why does the Speaker script begin with set [switch] to (0)?
Solution: A variable keeps its last value, even between runs. Setting it to 0 when the green flag is clicked makes every run start with the music off.
Summary
Sources and credits
The music-box project, the toggle scripts, the tracing tables and the practice items were written by Ganzaa.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
when this sprite clicked
if <(switch) = (0)> then
set [switch] to (1)
else
set [switch] to (0)
endwhen this sprite clicked
change [switch] by (1)
if <(switch) > (1)> then
set [switch] to (0)
endwhen green flag clicked
set [switch] to (0)
forever
if <(switch) = (1)> then
play sound (my tune) until done
end
end