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 onto 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 this stack, your sprite should glide onto the stage. Add a "when flag clicked" block and test the code. Notice that the designer sprite comes onto 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 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 dropdown, 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 your designer sprite. Right now, the code that makes the designer sprite come on stage is under the “When flag clicked” event. You’ll 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 dropdown. Now, when this sprite receives the "enter stage" message, it will run any code below the block, which will make it glide onto 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 under 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 on the stage and then moves to the middle of the stage in front of the model. Try the code. Great! 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. They can talk about what you learned about fashion design, what you want to design in the future, or thank the Gurus of CS First for providing this club. At the end of the day, you'll have an opportunity to share the project with your club Gurus, so if you do include a thank you message, make sure you show your project to your gurus. 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, "goto" block and "glide" block underneath the.
Add a "when flag clicked" block and a "hide" block so the designer isn't shown when the project starts.
Lastly, program your designer sprite to talk about what you've learned during this club and thank the gurus or 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.