Check out a free preview of the full Get Kids into Coding with Scratch course
The "Broadcasting Messages" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:
Steve demonstrates broadcasting messages and shows how sprites can listen for specific messages and execute code when they receive the intended message.
Transcript from the "Broadcasting Messages" Lesson
[00:00:00]
>> We're gonna take a few different things we talked about from the original link a little, I guess. The one we had with cheese puffs and an apple, and a little bit with, like we saw the broadcasting before and messages, we talked about that, and we saw a module.
[00:00:13]
We're gonna kind of pull some of those concepts together, and let you play around with that. You can either go to that very first thing we did with, in our case right now is cheese puffs. Could be anything. I have a version down here. In any given Scratch project, we can go in and remix it as well.
[00:00:29]
I'm gonna use just one that we had earlier, but again, there is a starter here if you don't have that one, either. So, this is again some of the initial code that we had. What I want to do is, like I said before, bunches of what you can make.
[00:00:42]
You can make games, you could also make full interactive stories. That when the green flag is clicked, it starts off a story where maybe one character walks in says something, the other character waits, then does the other thing, right? So there's gonna be a certain amount of like notified, you can try to get the timing perfect, where everything's always just the precise amount of time, or you can have various messages get broadcast, so on and so forth.
[00:01:05]
So if, for instance, we wanted to have the cheese puffs respond back. So let's say when I click Arnold, he says something, right? And then, when Arnold responds, then I want the opposite to happen, right? And so, or like I want the cheese puffs to respond after Arnold is done talking, right?
[00:01:32]
But Arnold can't control the cheese puffs, all Arnold could do is broadcast a message. So, let's see how that plays out. So we'll go over here, and we'll say, we'll just throw at a different event. We'll say, when this Sprite is clicked We'll say, hello for two seconds.
[00:01:52]
One Mississippi, two Mississippi great. But now I want the cheese puffs to respond, because what's the point of talking to cheese puffs if they don't talk back, right? So what we would do here is, we go ahead and we go into these events, which is, say hello for two seconds, then we gonna broadcast the message.
[00:02:14]
And I'm gonna add a new message saying, Arnold said hello. You can call whatever you want, right? It doesn't matter. Cool, so then what we're gonna do is, you're gonna say hello for two seconds, and then we are going to broadcast the message to the rest of the sprites that haven't said hello.
[00:02:35]
Here's the thing that broadcasts a message. If you broadcast a message and nobody's listening, it doesn't matter, right? Our event's firing the DOM all the time. So then, we need to program the opposite, which is, when the cheese puffs hear the message, they respond, right? What should the cheese puffs respond with?
[00:02:58]
Yes.
>> I'm Puffy. [LAUGH]
>> I'm puffy, all right. The cheese puffs will respond with, I'm puffy. Cool, so we'll go to the cheese puffs now, and we'll say, when I receive, Arnold said hello, we'll have them respond back, say, I am puffy For two seconds. Cool, so let's try it out.
[00:03:28]
We'll click on Arnold. Hello? I am Puffy. Right, so it's a way to like create interaction between two sprites by broadcasting a message. This could be, okay, if Evad catches Dave, broadcast game over, right? And throw a game over screen, which is a sprite that just overlays everything, it says game over, right?
[00:03:48]
Or if Dave eats ten tacos, broadcast you win. You win sprite, placed over the cat, well, I should do that in a later game, so don't worry. But there's various uses for building a story. For maybe if you wanna have buttons that do different things you can do, when the button is clicked, start the game, instead of the green flag, right?
[00:04:09]
So there's an initial screen, there's all sorts of stuff that you can do. But to do this, you need to kind of announce things to all the sprites, and they can listen for those things, and react accordingly, right? But you both need to send a message, and you need sprites that are ready to receive them.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops