In this video you'll learn how to use a broadcast block to communicate between sprites and make a designer sprite walk out on stage.
To start, add a designer sprite who will receive an award.
Just like on earlier days, you will create code to move the sprite on to the stage.
Move the sprite to a location where it will enter the stage.
Drag out a go to block.
Then move the sprite all the way forward on the stage and add a glide block.
When you test the stack your sprite should glide on to the stage.
Add a when flag clicked block and test the code.
Notice that the designer sprite comes on to the stage at the same time as the models.
Click on the model sprite.
From the events menu drag a broadcast block to the bottom of the model block stack.
Broadcast blocks allow one sprite to communicate a message to another sprite or the stage.
In this example the model sprite needs to tell the designer sprite that it is time to come on stage.
Click on the broadcast drop down, choose new message, then name the message something like enter stage.
Once the model has run all of its code it will broadcast the message, enter stage, to any other sprite that is waiting to receive that message.
Click on the designer sprite.
Right now the code that makes the designer sprite come on stage is under the when flag clicked event.
You want it to be under the when I receive block so that the sprite comes on stage after the models.
Drag out the when I receive block and replace the when flag clicked block with this new event.
You may need to choose the message, enter stage, from the broadcast drop down.
Now, when the sprite receives the enter stage message it will run any code below that block which will make it glide on to the stage.
Test your code.
The model sprites should enter the stage then the designer sprite should come to the front of the stage.
To make the designer appear after the models come on stage add a when flag clicked and hide block and snap them together.
Then add a show block immediately after the when I receive message block.
Reading this code you'll see that when the flag is clicked the designer sprite hides.
When the designer sprite receives the message, enter stage, the sprite shows itself, goes to the entry point of the stage, and then moves to the middle of the stage in front of the model.
Try the code.
The designer sprite is now coming to the front of the stage ready to speak.
Don't worry if you tested a solution and it didn't work!
That happens all the time in computer science!
Keep coding, testing, and trying solutions until you find one that works!
You can make the sprite say anything you want.
Now it's your turn.
Add a broadcast block to the model code and name the message.
Add a new sprite as your designer sprite.
Add a when I receive block to the designer with a show block, go to block, and glide block underneath.
Add a when flag clicked block and a hide block so the designer isn't shown when the project starts.
Finally, program your designer sprite to talk about what you've learned and to thank others who have supported you.
You can also program your sprite to move around the stage.
- Add a sprite.
- Program a starting and an ending position for the designer sprite, and an event that will start your program.
- Broadcast a message to tell the designer sprite to enter the stage.
- Program the designer sprite to receive the message to enter the stage.