Sending and Receiving Broadcasts

Because Scratch applications can be made up of many different sprites, each of which may consist of many different scripts, coordinating the activity of all the different parts of the application can be challenging. By providing access to the three code blocks shown in Figure 9.5, Scratch offers the ability to send and receive broadcast messages as a means of coordinating script execution.

Figure 9.5. Broadcast messages provide the capability for one script to notify other scripts that an event has occurred.


Using the first two code blocks shown in Figure 9.5, you can pass messages to any script within an application that begins with the hat code block shown at the bottom of Figure 9.5. For example, the following script demonstrates how to send a broadcast message of jump to all sprites within the application:

To specify the message sent by the control code block, all you have to do is click on the block’s drop-down list and then either select a previously typed message or create a new message by clicking on New and then typing in the message. This particular code block sends its message and then allows the script in which it is embedded to continue executing. Alternatively, the following script not only sends a broadcast message but also waits until every script in the application, which has been set up to execute when the message is sent, has finished executing:

Using the hat block, you can set up a script to execute whenever a specified message is received.

Note

Using the three previous scripts, you could create a new application made up of two button controls and the default Cat sprite. By assigning the first script to the first button sprite, the second script to the second button sprite, and the third script to the Cat sprite, you can make the Cat sprite jump up and down on the stage any time you click on one of the button sprites.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.96.5