Week 24 · Unit 10: Be a game developer · 3 lessons
3
Sprites send messages
Computing 6: Unit 10, pp. 134–149
GoalUse broadcast and receive blocks to make several scripts work together in a planned order.
New words
broadcast · to send a named message to all the sprites in a projectreceive · to start a script when a message arrives, using when I receivemessage · a named signal that one script sends and other scripts wait formultiple algorithms · several scripts or sprites that work together, linked by messages
Explanation
In a game, different sprites have their own algorithms, and these must work together. Messages connect them. The broadcast block sends a named message to every sprite. A script that starts with when I receive and the same message name now begins to run. If several scripts wait for the same message, they all start together. The script that sent the message carries on with its next block at once. To trace a program, follow the messages like arrows from sender to receiver. A message name must be spelt the same in both places, or the receiver never starts.
Worked examples
Time
Who
What happens
0 s
Starter
says Ready? for 1 second
1 s
Starter
says Go! for 1 second
2 s
Starter
broadcasts go
2 s
Frog and Snail
both receive go and start moving
Follow the timeline for the race program. Nothing happens to the frog and the snail until the message goes out. Then both start at the same moment.
Starter broadcasts go→Frog and Snail both start→Frog ends its loop→Frog broadcasts finished→Starter says: We have a winner!
Messages can pass the baton from sprite to sprite. Here the frog finishes its loop, then broadcasts finished, and the Starter reacts to that second message.
Broadcast
When I receive
Does the receiver run?
go
go
yes
go
start
no, the names are different
finished
finished
yes
lights on
lights
no, the names are different
A message must have the same name where it is sent and where it is received. This table shows four pairs and what happens.
Class activity
Human messages (no computer): the players are sprites, and each has a script card. Card 1: ‘when the bell rings: say Ready, then broadcast set’. Card 2: ‘when I receive set: crouch, then broadcast go’. Card 3 and Card 4: ‘when I receive go: run to the wall’. Act it out. Which players start together? Then change one message on a card to a different name and run it again. What happens? If you have a computer or tablet, build the race program in Scratch.
Practice
1
Write the order in which the words are said, and how long the whole program takes.
Owl says Good morning! (2 seconds), then Hen says Cluck! (1 second), then Owl says Yum! (2 seconds). The total is 2 + 1 + 2 = 5 seconds.
2
Three sprites each have a script that begins with when I receive [go]. How many scripts start when go is broadcast, and when do they start?
All three scripts start, at the same moment.
3
Which of the receivers never run? Explain.
Pair
Broadcast
When I receive
1
stop
stop
2
end
finish
3
music
music
4
bell
ring
Receivers 2 and 4 never run. Their names are different from the broadcast names (end and finish, bell and ring).
4
In a quiz game, the Quiz sprite checks an answer. The Scoreboard sprite must add 1 point when the answer is right. Which message would you use, and where is it sent and received?
For example the message right answer. The Quiz sprite broadcasts right answer after it checks the answer. The Scoreboard has a script that starts with when I receive [right answer] and then changes score by 1.