Scratch for Kids

A Watch the course on Frontend Masters

Broadcasting

As we’ve seen, all of the code lives inside of a sprite. A sprite can tell if it’s near another sprite, but it can’t tell it waht to do. (It can trigger a clone of another sprite, but it can’t control how that clone behaves.)

We can do broadcast a message. Any other sprites that care about that message can listen for messages and then run their own code.

Consider the above example. It would be very polite if the monkey responded, right?

The current code looks like this:

when this sprite clicked
say [Hello!] for [2] seconds

We can see this happening, but the monkey has no idea it happened.

We can add a broadcast block to our sprite.

when this sprite clicked
say [Hello!] for [2] seconds
broadcast (Cat Said Hello v) // The message name doesn't matter.

On the Monkey, we’ll add this new block.

when I receive [Cat Said Hello v]
say [Hi hi hi!] for [2] seconds

Next, let’s look at Variable Lists.