AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Sprites that interact

Week 1 · Unit 1: Be a designer · 3 lessons
2

Sprites that send messages

Computing 5: Unit 1, pp. 8–19
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.

OwlwhenclickedsayShall we sing?for2secondsbroadcastsingand waitsayWell done!for2secondsFrogwhen I receivesingsayRibbit!for3seconds
Worked examples
SecondsWho speaks
0 to 2Owl: Shall we sing?
2 to 5Frog: Ribbit!
5 to 7Owl: 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.
OwlwhenclickedsayShall we sing?for2secondsbroadcastsingsayWell done!for2secondsFrogwhen I receivesingsayRibbit!for3seconds
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.
OwlwhenclickedbroadcastsingFrogwhen I receivesingsayRibbit!for2secondsHenwhen I receivesingsayCluck!for2seconds
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’?
2
Say when the receiver starts and when the whole program ends.
SenderwhenclickedsayReady?for1secondsbroadcaststartsayGo!for2secondsReceiverwhen I receivestartsayOkay!for5seconds
3
Ravi’s program does nothing when the green flag is clicked. Find the bug.
OwlwhenclickedbroadcastsingFrogwhen I receivesignsayRibbit!for2seconds
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.