Howardism Musings from my Awakening Dementia
My collected thoughts flamed by hubris
Home PageSend Comment

The difficulty with this conversation is that it's very different from most of the ones I've had of late. Which, as I explained, have mostly been with trees.

–Douglas Adams

Passing Messages with Broadcasts

Let's take the two sprite characters we created in the previous lesson on animation and have them "talk" to each other. The idea is to have them say something, but also to listen in order to respond. We'll tackle the "speaking" part first.

Speaking Sprites

Don't get your hopes up, for when a sprite "says" something, it does this using a speech bubble– like a comic strip. It is easy to put a speech bubble on a sprite by using one of the Say bricks.

Say with Timer

There are two different Say bricks in the Looks brick group.
Has a sprite "speak" a phrase in a speech bubble for 2 seconds and then the speech bubble goes away.
The first brick has the sprite say something for a given number of seconds and then stops. What the sprite speaks is written in a speech bubble like a comic strip.

For instance:

Image with a speech bubble

Say without Timer

The other Say brick
Has a sprite "speak" a phrase in a speech bubble until told otherwise by using a say brick with an empty message.
does the same thing, except that the speech bubble doesn't automatically go away after some time. The speech bubble is replaced if another Say brick is used.

You can make the speech bubble go away completely if you use the Say brick with an empty phrase.

Speaking Example

With a sprite selected and the "Scripts" tab opened, drag the following bricks to build this speaking example program:

Example program for making an image speak

Once the program is built, press the "1" key to make the image say, "Quack, quack." Notice how the bubble will stay there until either "2", "3" or "4" key is pressed.

If the "4" key is pressed, the speech bubble is displayed, but soon goes away.

Now let's try to have a dialog between two sprites.

Dialog Example

Using the project program we built in the previous example, add or create a Example program for making two images carry on a conversation. second sprite, and with its "Scripts" tab opened, drag the following bricks to build the code shown in the margin:

When either the 1, 2, 3 or 4 keys are pressed, the first image makes a statement. In 2 seconds, the other image replies to that statement.

This is accomplished by having each sprite respond to the "1" key when it is pressed, but the second image purposely has a Wait 2 seconds brick to make it appear as if the two images are carrying on a conversation.

Without the wait, the user reading the dialog would not be sure which image "spoke" first and which spoke second.

Images with a speech bubbles

Listening Sprites

A sprite doesn't actually respond to a speech bubble– those are only for human viewers. However, a sprite can send and respond to silent messages sent behind the scenes. This allows us to have one sprite alert one or more sprites about something, in our case, when a sprite has said something.

In the illustration below, the green duck sprite sends a "message" out for the yellow duck to receive and respond. The message isn't sent to a particular sprite, instead it is broadcast to all sprites, and any sprite can listen for a particular message.

Broadcast Message

Similar to the Say bricks, there are two different Broadcast bricks in the Control brick group.
Has a sprite send a message to all other listening sprites.
Let's talk about the brick that has broadcast printed on it.

When the broadcast brick is executed, it sends a "message" out to all the sprites in our project (the stage listens too). Another sprite can trigger some bricks to be executed whenever a particular message has been heard.

Code example of a say and broadcast bricks Begin by dragging a broadcast brick into the Scripts area and attach it to a say with timer brick. Now click the black down-arrow. A menu of all possible messages are listed. At first, we haven't created any, so only the "New…" item is available. Select New.

A little window will appear allowing us to specify a new message we wish to broadcast. The message can be anything you can type on the keyboard. It helps if the message is meaningful, however.

Type: Spoke: Quack, quack and click OK.

Running this sequence of bricks won't obviously make a difference. If a broadcast is sent in a forest of sprites, but no one is listening, did the broadcast happen? Sorry, I couldn't resist.

Select the other sprite that should answer by clicking on the sprite's image in the Sprites panel in the bottom right-hand corner. Go to the Control brick group and you'll see a trigger brick labeled: When I receive.


Has a sprite trigger some code based on someone sending a particular message.
Drag this brick into Scripts tab area. You'll notice that it has a menu, and when you click the black arrow, a list of all messages shows up. However, you may notice our Spoke: Quack, quack message is already listed. Very convenient!

Code example responding to a broadcast Attach the say brick that responds to "Quack, quack." to this trigger brick. Choose the say brick that waits for 2 seconds.

Now pop back over to the first sprite and double-click on the code that sends out our message. The first sprite speaks followed by a response from the second sprite.

To continue the dialog, we could have the second sprite send a message that the first sprite receives, and we could continue this way for the entire "conversation". Each sprite is broadcasting messages to the other, and this approach essentially gets two sprites to act like one. It helps this example if I put the code from each sprite side by side:

Code illustration of the call and response of broadcast messages.

Broadcast and Wait

In the previous example, each sprite waited until the other sprite had awakened it. You'll notice in the example code that the broadcast brick was the last thing it would do in each of the brick sequences. It was as if the sprite would go to sleep until a broadcast message would wake it.


Broadcasts a message and then waits for any answering sprites to finish their brick sequences before continuing.
Another approach to the same problem is to have one sprite act like a controller and broadcast messages to another sprite. The difference is that the first sprite waits until the second sprite has acted. This uses the broadcast and wait brick.

The broadcast message is sent just like what we've seen above, but instead of continuing to the next brick in its sequence, it waits until all answering sprites have completed their code brick sequences.

Let's rewrite our previous project using the broadcast and wait brick. I will put the code for each sprite side-by-side so that you can see that the call and response is still there, but the first sprite is clearly the guy in charge and controls how the action unfolds.

Code illustration of the call and response with broadcast and wait messages.

Continue by viewing the Lesson Plan for this lesson, or return to the previous lesson.
Tell others about this article:
Click here to submit this page to Stumble It