Jump to content

Scratch/Lessons/Event Triggers

From Wikibooks, open books for an open world

In order for the user to be able to interact with your Scratch project, you must use event triggers. Event triggers activate code when a specified event happens, such as a key being pressed or a button being clicked. Understanding event triggers is essential to creating a Scratch project. We will go over some event triggers here.

The "When Green Flag Clicked" Block

[edit | edit source]
The "When Green Flag Clicked" block

At the top left of the stage is a green flag. In order for the Scratch program to start, this green flag must be clicked.

Using the "When Green Flag Clicked" block, you can make anything you want happen when the green flag is clicked. When the green flag is clicked, any "When Green Flag Clicked" blocks will trigger. For example, when the green flag is clicked, the cat sprite will wait for a second, move to the right, wait a second, then move to the left forever:

The "When Green Flag Clicked" block is certainly a useful block, but without other event triggers your users would not be able to interact with your program; they would only be able to start it. Let's take a look at some other event triggers.

The "When This Sprite Clicked" Block

[edit | edit source]
The "When This Sprite Clicked" block

In order to create buttons and the like in your Scratch program, you must use the "When This Sprite Clicked" block. When the sprite is clicked, this block activates code. For example, whenever the cat sprite is clicked, it rotates clockwise fifteen degrees: