Broadcasting a Click Message

In this activity, you will use messages to tell other parts of your program what to do when the user clicks your click area.

Since your project will react to clicks on the stage, you'll need to create code for the stage. You can find the stage scripts by clicking the "Stage" icon in the lower right corner of the Snap! window. (You can return to your sprite code by clicking a sprite icon.)
screenshot of lower left corner of Snap! window showing the stage icon and two sprite icons

The code for the stage controls only the stage (such as its background). The code for each sprite controls only that sprite (such as its costume or movement). If you want clicking the stage to make a sprite do something, then you need to make the stage talk to the sprite.

In Snap!, messages let one part of your program talk to another part of your program. The broadcast block sends a message to every part of your project. You can set the message to anything that will help you remember what it means. Here's an example:
broadcast (game ready to start)

Once the message has been sent, any scripts in your project that begin with the when I receive block with that same message will run, even if those scripts belong to one of your sprites.
when I receive (game ready to start)

  1. Experiment with broadcast and when I receive in a new Snap! project. Try making one sprite tell several other sprites what to do.
  2. Open your "Sparks - Interactive Project."

In general, you can use broadcast and when I receive in either the code for the stage or the code for any sprite. Here, you are creating code for sprites to respond to clicks on the stage, so you'll use broadcast in the code for the stage and when I receive in the code for your sprites(s).

When the user clicks the stage, the code for the stage will broadcast a message to the rest of your program, and your sprite(s) will use when I receive to listen for messages.

  1. Create a script for your stage that broadcasts a message when the stage is clicked. (This is the script that will trigger sprite behavior when the user clicks your click area on the stage, so it has to be a script for the stage. Make sure the stage icon is clicked in the lower right corner of your Snap! window.)
  2. To set the message, click the down arrow and select "new..."
    broadcast block with menu open showing one option: new...
  3. Create a script for your sprite(s) that responds to that message. You'll need to decide:
  4. Check that your project does what you want when you click the click area and that it doesn't do it when you click somewhere else.
  5. Now Is a Good Time to Save
  1. Set up more messages and code to respond to other click areas.
In this activity, you created click areas that use clicking different parts of the Snap! stage as events that tell the computer to do something.