🗣 Events & Listeners | myBetabox

🗣 Events & Listeners

Events and listeners are another extremely important part of computer science, and will be especially useful in creating your animated stories.

Events are anything that can happen during our animation.

Listeners are pieces of code that wait for those events and then run when they happen.

Let’s try think of some real-life events and listeners. Some events could be:

  • A door opening
  • The lights turning off
  • Stomp your feet twice

And the listeners might look like this:

  • Listen for: a door opening THEN the dog barks
  • Listen for: a light turning off THEN you go to sleep
  • Listen for: feet stomping twice THEN we all clap our hands

In these examples, we are waiting (or listening) for some event to happen, like a door opening, then responding with some action.

In Scratch, there are lots of built-in events in the yellow Events section. Let’s take a look at a few of them:

  •  We’ve used this one a bunch of times already. This listens for the green flag button to be clicked, then runs code when it happens.
  •  This event will listen for the loudness (or volume) of the user to be a certain amount, then do something when that happens.
  •  This event will wait for a certain backdrop to show, and then run some code when it does.
  •  This waits for a certain message, and when it gets that message, some code will run. This is how we can create custom events in Scratch — for example, if we wanted something special to happen every time our character says something, or if we want to have multiple sprites listen for the same message. This block is usually paired with  for sending the messages.

Here’s a quick example using the Loudness event listener.

When your microphone is enabled, the sprites will listen for loudness, or volume before doing or saying the next action. The code for the above animation looks something like this:

A video example of how to use this code can be found in the Final Presentations section.