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

Game building blocks

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.

StarterwhenclickedsayReady?for1secondssayGo!for1secondsbroadcastgoFrogwhen I receivegomove50stepsrepeat4Snailwhen I receivegomove20stepsrepeat4
Worked examples
TimeWhoWhat happens
0 sStartersays Ready? for 1 second
1 sStartersays Go! for 1 second
2 sStarterbroadcasts go
2 sFrog and Snailboth 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.
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.
BroadcastWhen I receiveDoes the receiver run?
gogoyes
gostartno, the names are different
finishedfinishedyes
lights onlightsno, 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.
OwlwhenclickedsayGood morning!for2secondsbroadcastwake upHenwhen I receivewake upsayCluck!for1secondsbroadcastbreakfastOwl (second script)when I receivebreakfastsayYum!for2seconds
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?
3
Which of the receivers never run? Explain.
PairBroadcastWhen I receive
1stopstop
2endfinish
3musicmusic
4bellring
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?