2026年1月26日月曜日

How to use Messages (Broadcasts) in Scratch

 My apologies for the mix-up! Here is the detailed explanation of how Sprites communicate using Messages (Broadcasts) in English.


Communication Between Sprites: The "Broadcast" System

In Scratch, Sprites are independent entities. To make them coordinate with each other or "talk," we use the Broadcast system. This is a perfect example of Message Passing in Object-Oriented Programming.

1. The Core Blocks

You can find these in the Events (yellow) category:

  • broadcast [message1]: This sends a signal to every single Sprite and the Stage. The Sprite sending the message immediately continues to its next block.

  • broadcast [message1] and wait: This sends a signal and "pauses" the sender's script. The sender will only continue once every Sprite that received the message has finished their specific tasks.

  • when I receive [message1]: This is the "Listener." It tells a Sprite to start a script only when that specific message is "heard" in the air.


2. Practical Example: A Simple Conversation

Let’s look at how a Cat and a Dog might have a timed conversation.

Cat's Script (The Starter):

コード スニペット
when green flag clicked
say [Hello, Dog!] for (2) seconds
broadcast [dog_turn]  // Sending the signal to the Dog

Dog's Script (The Listener):

コード スニペット
when I receive [dog_turn] // Waiting for the Cat to finish
say [Hi, Cat! How are you?] for (2) seconds
broadcast [cat_reply]  // Sending the signal back to the Cat

Cat's Script (Continuing):

コード スニペット
when I receive [cat_reply]
say [I'm doing great!] for (2) seconds

3. Why Use Broadcasts? (OOP Benefits)

  • Decoupling: Sprites don't need to know exactly how other Sprites work. They just say, "I'm done!" (Broadcast), and others react.

  • One-to-Many Communication: One broadcast can trigger 10 different Sprites at once. For example, a Game Start broadcast can make enemies appear, start the music, and reset the timer simultaneously.

  • Triggering State Changes: You can use messages like Level Up or Game Over to tell the Stage to change its Backdrop.


4. Best Practices for Messages

  1. Use Descriptive Names: Avoid "message1." Use names like start_countdown, enemy_hit, or close_menu.

  2. Avoid Broadcast Loops: Be careful not to have Message A trigger Message B, which then triggers Message A again, or your game might crash!

  3. Global Signal: Remember that the Stage can also receive broadcasts, making it perfect for changing scenes or playing background music.


Summary Table: Messaging in Scratch

FeatureConceptDescription
BroadcastEvent TriggerTelling the world that something happened.
When I ReceiveEvent HandlerA specific reaction to a signal.
Broadcast and WaitSynchronous CallEnsuring Task A finishes before Task B starts.

📋 Copy Icon: You can copy this explanation to help organize your project's logic!

Would you like me to show you how to use a Broadcast to specifically trigger a Backdrop change when a player reaches a certain score?