Broadcasting and Receiving Messages
-
Prerequisite
What is a Message?
A message is some information sent by one person to another person.
For example, a teacher says “sit down”, and a student hears it. Here “sit down” is a message. The teacher is the “sender” of the message, and the student is the “receiver”.
Broadcasting a Message
When the same message is sent to everyone at the same time, we say the message has been “broadcast”.
For example, the teacher says “sit down”, and all the students in the room hear it at the same time.
Now let’s look at the blocks for working with messages.
“Broadcast (message)” and “When I Receive (message)”
Most of the time, we use these 2 blocks to work with messages. Each of them only takes one input, which is the message to send or receive.
When you broadcast a message, all sprites will receive this message at the same time, including the sender sprite itself. The stage will also receive this message. They can choose to ignore the message, or do something when they receive it. If they choose to do something, they can use the “when I receive” block, and attach other blocks below it.
For example, suppose there 3 sprites in a project: Cody, Apple and Beachball. We add the following code to them.When we click the green flag button, the Cody sprite will broadcast “message1”, and it will turn right when it receives this message. The Apple sprite will turn left when it receives this message, and the beachball sprite will ignore this message. The stage will change its backdrop when it receives this message. Here is the result:
Define a New Message
Instead of “message1”, we can define a new message type. It will make it easier to read your program if we know the meaning of the message.
For example, we can add a new message “turn”, and use that instead of “message1”.
Similarly, we can define another message “move”, which tells the sprites to move. This time, only Cody and Beachball will handle the “move” message.
Delete a Message
If you want to delete a message from the dropdown, such as “move”, you just need to make sure no block is broadcasting or receiving that message, then that message will be removed the next time you load this project.
Broadcast and Wait
After a sprite broadcasts a message, it would usually move on to the next block right away. However, sometimes we need to make the broadcasting sprite wait until all receiving sprites have finished processing that message.
For example, suppose the Apple sprite will turn 50 times when it receives the “turn” message, so it will take a while to finish. When we run the project, the Cody sprite will send out the “turn” message, then immediately send out the “move” sprite. You will see that the Beachball sprite starts to move up right away, before the Apple sprite finishes turning.
Suppose we do not want to broadcast the “move” message until every sprite has finished turning. We can use “broadcast and wait” for the “turn” message. This way, Cody will wait for all sprites to finish processing the “turn” message, and then broadcast the “move” message.