GoalUse broadcast and ‘when I receive’ so that one sprite can start a script in another sprite.
New words
broadcast · to send a message to all the sprites in a projectmessage · a name that sprites send and receive, such as sing or gowhen I receive · a block that starts a script when its message arrivesbroadcast and wait · a block that sends a message and waits until the receiving scripts have finished
Explanation
Sprites can send messages to each other. A message is only a name, such as sing. The broadcast block sends the message to every sprite. A script that begins with ‘when I receive’ and the same message name starts as soon as the message arrives. The two names must match exactly. Plain broadcast does not wait, so the sender carries on straight away. The block ‘broadcast and wait’ makes the sender wait until the other scripts have finished. Use it when the sprites must take turns.
Worked examples
Seconds
Who speaks
0 to 2
Owl: Shall we sing?
2 to 5
Frog: Ribbit!
5 to 7
Owl: Well done!
With ‘broadcast and wait’ the sprites take turns. The owl asks for 2 seconds, the frog speaks for 3 seconds, then the owl speaks for 2 seconds. The whole program takes 2 + 3 + 2 = 7 seconds.
With plain broadcast the owl does not wait. At second 2 it sends the message and at once says ‘Well done!’ for 2 seconds. The frog speaks from second 2 to second 5 at the same time. Now the program ends at second 5.
One message can start several sprites. When the owl broadcasts sing, the frog and the hen both start their scripts at the same moment.
Class activity
Message cards (3 or more players). Two players are sprites. Each holds a card, for example ‘When I hear CLAP: stand up, turn round, sit down’. A third player is the director who claps (broadcast). Try it once. Then try ‘broadcast and wait’: the director must wait until both players have sat down before saying ‘Finished!’. What goes wrong if the clap is a different word from the card? If you have a computer or tablet, build the owl and frog program in Scratch.
Practice
1
What is the difference between ‘broadcast’ and ‘broadcast and wait’?
Plain broadcast sends the message and the sender carries on at once. ‘Broadcast and wait’ makes the sender wait until the receiving scripts have finished.
2
Say when the receiver starts and when the whole program ends.
The receiver starts at second 1, when the message is sent. The sender finishes at second 3. The receiver speaks from second 1 to second 6, so the program ends at second 6.
3
Ravi’s program does nothing when the green flag is clicked. Find the bug.
The hat block of the frog waits for ‘sign’, but the owl broadcasts ‘sing’. The two message names do not match, so the frog never starts.
4
A dog says ‘Fetch!’ and then sends a message. The ball must glide to a random position when it gets the message. Write both scripts in words.
Dog: when the green flag is clicked; say Fetch! for 2 seconds; broadcast fetch. Ball: when I receive fetch; glide 1 secs to random position. The message name must be the same in both.