Consica Labs

Consica Labs
Chapter 7

Events

Starting scripts with inputs and flag triggers

Definition

Event blocks are hat-shaped triggers that start execution threads when inputs (clicks, key presses) occur. Key concepts include Hat Block.

Think of Events as:

Director script
Grid movements
Trigger alarms
Costume racks

Just as an actor changes costume or walks across the theater grid on cue, Events dictates sprite behavior.

Real-Life Example

Just as you coordinate actions in real life, Events works by structuring logic commands in sequence:

  1. 1 Register the trigger event block.
  2. 2 Run calculations or movements within a continuous control block.
  3. 3 Update values or graphics on the stage viewport.

Key Highlights:

  • Trigger event
  • Logic evaluation
  • Visual update

Interactive Diagram

Launch the interactive diagram to see this in action.

Open Interactive Diagram

The interactive diagram for this chapter demonstrates Events. It shows event blocks (when green flag clicked, when key pressed, when sprite clicked) triggering scripts.

What to explore:

  • click the green flag; press keys; click the sprite — watch different scripts start running based on events
  • event blocks start scripts when something happens — a flag click, key press, or sprite interaction triggers the code below

Introduction

In Scratch, nothing happens until you tell it to start. This is where Events come in. Event blocks are like triggers — they wait for something to happen and then run a script in response. Clicking the green flag, pressing a key, clicking a sprite, or receiving a broadcast message are all Events that can start scripts. Without Events, your sprites would just sit there, waiting for instructions.

Events are fundamental to programming because they handle the 'when' of your program. They control the flow of execution, deciding which code runs and when. Scratch provides several types of Events: user-initiated (green flag, key press, sprite click), time-based (broadcast and wait), and system Events (when backdrop switches to, when loudness > 10). Each type responds to a different trigger.

In this chapter, you will learn how to use each event block, understand the difference between Events and other control structures, and design event-driven programs that respond to user input and system changes. By the end, you will be able to create interactive projects that react to the user and to other sprites.

How It Works

The most common event block is 'when green flag clicked'. Any script attached to this block runs when the user clicks the green flag above the stage. This is the standard way to start a project. You can have multiple scripts all starting with 'when green flag clicked' — they all run simultaneously, or in parallel. This is Scratch's approach to multitasking.

The 'when () key pressed' block triggers a script when a specific keyboard key is pressed. You can choose from any key on the keyboard, including letters, numbers, arrow keys, and space. This is how you create keyboard-controlled movement. The 'when this sprite clicked' block triggers when the user clicks on the specific sprite. This is useful for interactive buttons and clickable objects.

Household Object Analogy

Think of event blocks like doorbells. A doorbell button (the event) waits for someone to press it (the trigger). When pressed, it sends a signal that causes something to happen — someone opens the door, a bell rings, a light turns on. Similarly, event blocks wait for a specific trigger (green flag click, key press, sprite click) and then start a script in response. Different triggers cause different responses.

Deeper Dive

The 'broadcast ()' and 'broadcast () and wait' blocks are Scratch's way of sending messages between sprites. A broadcast is like a public announcement — every sprite (and the stage) can hear it and respond. Any script that starts with 'when I receive ()' for that specific message will run. This is how you coordinate actions between multiple sprites. For example, one sprite might broadcast 'level complete', and multiple other sprites might respond by showing congratulations, playing a sound, and resetting positions.

The 'broadcast () and wait' block sends the message but pauses the sending script until all receiving scripts have completed. The regular 'broadcast ()' fires and forgets — it sends the message and immediately continues, regardless of what receivers do. This distinction is important for timing. If you need to ensure all receivers finish before proceeding, use 'broadcast and wait'.

There is also a special broadcast message called 'new message' that lets you create custom message names. Common message names include 'jump', 'game over', 'next level', 'collect coin', and 'enemy hit'. Using descriptive message names makes your program easier to understand. You can create as many different messages as you need.

Key Insight

Multiple scripts can start from the same event, and they all run at the same time. This is called parallel execution. Scratch handles this by rapidly switching between scripts, giving each one a tiny slice of processing time. This creates the illusion of simultaneous execution, just like a computer running multiple programs at once.

Advanced

The 'when () key pressed' event can capture physical keyboard input. But Scratch also supports Sensing Block like 'key () pressed?' that you can use inside loops to continuously check if a key is held down. The difference: 'when key pressed' fires once when the key is first pressed, while a loop with 'key pressed?' can detect held keys for continuous movement. Most games use the loop approach for smooth, responsive controls.

Event-driven programming is a major paradigm in software development. Scratch's event model mirrors event-driven architectures used in professional frameworks like JavaScript's addEventListener, Python's asyncio, and GUI toolkits like Tkinter and Qt. Learning event-driven thinking in Scratch prepares you for event-based programming in any language.

The order of event handling matters. If two Events trigger at nearly the same time (for example, pressing a key while a broadcast is being received), Scratch processes Events in the order they occurred. However, scripts triggered by the same event run in parallel through rapid interleaving. Understanding this concurrency model helps you design projects that behave predictably.

Vocabulary Table

Term Definition
EventsThe primary technological concept explaining how components interact within the context of Scratch Programming.
Coordinate GridThe X and Y plane representing the Stage layout coordinates.
Hat BlockA block with a rounded top that registers events to launch scripts.
Sensing BlockA block that checks for collisions, touch parameters, or mouse pointer coordinates.

Fun Facts

The green flag event was inspired by the 'play' button on media players. It is a universal symbol for 'start' that children recognize from an early age.

Scratch supports up to 26 different key press Events (A-Z), plus arrow keys, space, and number keys.

The 'when backdrop switches to' event can trigger scripts based on backdrop changes, allowing you to run setup code whenever the scene changes.

Broadcast messages can be sent and received by any sprite, including the stage, making them Scratch's primary inter-sprite communication mechanism.

The 'when loudness > 10' block (available with the microphone) allows Scratch to respond to real-world sound levels, enabling sound-activated projects.

Common Misconceptions

Misconception: Only one script can start from the green flag.

Truth: Any number of scripts can start from the green flag. They all run simultaneously in parallel, which is how Scratch implements multitasking.

Misconception: Key press Events detect keys being held down.

Truth: The 'when key pressed' block fires once when the key is first pressed. To detect held keys, use a loop with the 'key pressed?' Sensing Block.

Misconception: Broadcasts only work between sprites on the same project.

Truth: Broadcasts are local to each project. A broadcast from one project cannot trigger scripts in another project.

Misconception: Events and Control blocks are the same thing.

Truth: Events start scripts (they are triggers), while Control blocks manage the flow within scripts (loops, conditions, waits). They work together but serve different purposes.

Knowledge Check

1. What is the main role of Events?

Answer: To orchestrate behaviors and controls visually

2. Which block shape starts a script stack in Scratch?

Answer: Hat block (curved top)

3. What is the maximum X coordinate limit on the stage?

Answer: 240