3.4 Communication between sprites ⭐

View

In this lesson, you will gain experience in synchronizing interaction between sprites. To enable two sprites to communicate, we will use Sensing and Broadcast commands. Broadcasts send information in other blocks of code. In addition, you will be more familiar with the use of events and parallelism gained in module 1.

Broadcast command

The command which makes two sprites communicate with each other is “Broadcast”.  Broadcast is used to send a message:


 The response of the recipient of the message is triggered by the block: “When I Receive”:

In the following exercise, we will make two sprites have a simple chat, by using the blocks Broadcast and When I Receive.

Step 1. Add a bear and a beetle from the sprites overview. If you want to change their position, click on Costume tab, and select      according to the position you want.

Step 2. Add the following code to the Bear sprite:

 

Figure 85: The script for the bear

Step 3. Add the following code to the Beetle sprite:

Figure 86: The script for Beetle sprite

Step 4. Run the program.  You should get the following result:

 

Figure 87: Bear broadcast message to Beatle and Beatle responds

Last modified: Tuesday, 1 March 2022, 1:05 PM