Event Programming

Control blocks can initiate script execution, which is critical to the execution of Scratch applications. This is accomplished with hat blocks, including those shown in Figure 9.1.

Figure 9.1. Hat blocks automate the execution of scripts.


As you have seen in many examples in this book, the first code block shown in Figure 9.1 initiates a script’s execution whenever the green flag button is clicked, and it is the most common means of starting an application’s execution. For example, if you were to add the following script to any sprite or background in a Scratch application, it would automatically play a specified audio file (provided that file has been imported).

The second code block shown in Figure 9.1 initiates a script’s execution whenever a specified keyboard key is pressed. The key that is used as the trigger is selected by clicking on the code block’s drop-down list and making a selection of one of the following keystrokes:

  • Up, down, right, and left arrow keys

  • The spacebar

  • a – z

  • 0 – 9

For example, the following script demonstrates how to move a sprite by 50 steps whenever the keyboard’s spacebar is pressed:

The third code block shown in Figure 9.1 initiates script execution whenever the sprite to which it belongs is clicked. The following script demonstrates how to use this code block to automate the display of text in a speech bubble whenever the sprite to which it has been added is clicked:

Note

Scratch provides a fourth hat control block, which is covered later in this chapter. This code block is used to initiate script execution when broadcast messages are received.


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

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