stoneskin

learning coding with Scratch , Python and Java

8 Events

8.1 Event Driving Programming

Event Driving Programming is programming n which the code is based on events.

For example, the program could detect the user actions such as “a mouse click” and “Key pressed”, or a sensor output, or a message from other programs. when those events be received, the programming will run or doing something.

events driving programming

In scratch, The Events related blocks are blocks with “When ..” and the message broadcasts blocks.

scratch events blocks

You already know all sprits could triggered by the Events of “When Green flag click” “When Key pressed” “When the sprits clicked”. Those are build-in events blocks.

Next we we lear how to build custom event with message broadcast block.

8.2 Broadcast message block

We can build a “Start” button: startbutton

And put when the sprite clicked and broadcast message to it. create new message

Name the new message “StartGame”. create new message

The final code of StartGame button startgame

And you could have multiple sprites to take action by the “StartGame” event event example

see the example https://scratch.mit.edu/projects/446689602

8.3 Broadcast message and wait block

This block also send message out, but it wait all receiver finish the running and continue the next.

Make a button “let’s dance” dance

Add code let the cat dance for some time cat dance

The button will hidden after click, then send broadcast message. But the next code after will not run until the received code completed, Then It will run next block after broadcast again again.

check the example https://scratch.mit.edu/projects/446699823

8.4 Multi-threading

Add another sprite in previous project, to receive the dance event. another

And you could use a variable to control other sprits action. See example: https://scratch.mit.edu/projects/446703100/

8.5 Project

Make a game using custom event and message broadcast.