They are typically used to pass messages from one sprite to another.
Imagine a play where a director yells "now, it's time for jumping" and some actors start jumping. The difference is that in Scratch any sprite can yell anything at anyone else.
The tic-tac-toe sample project makes use of broadcast. Maybe you could look at the code and get an idea from there.
Also, in this project http://scratch.mit.edu/projects/andresmh/645
I use the broadcast in the sprite called 'andres' to tell the stage to start changing colors.
I hope this helps.
Offline
Messages are useful for a lot more than just inter-sprite communication. They can be used to synchronize simultaneous actions in a single sprite (a changing costume loop, a glide, and and think statement to walk across the screen while thinking, for example).
They can also be used to correct for major holes in the scratch language.
For example, to make the missing while-loop construct, replace
while <cond> do <body>
with
broadcast while-loop-1 and wait
when message while-loop-1
forever
if <cond>
<body>
else
Stop script
This is an ugly kluge, and I can't wait for them to put in a proper while loop, but in the meantime ...
Offline
I believe the block labelled "forever if" is a while loop-- the name confused me at first, but I'm playing with at the moment and I'm pretty sure that's what it does.
It seems to me the message passing allows Scratch to be a lot more powerful than it looks on the surface. You can use it to implement functions, which can pass each other parameters & responses through global variables, at least..
<3
Offline
No, the forever-if loop is a forever loop with an embedded "if" or "wait".
You want to use "repeat-until" to model while loops.
The message passing is quite powerful, though it would be more powerful if you could attach parameters to the messages, direct the messages to specific sprites, and wait for messages.
Offline
How do you get a sprite to do something if touches another sprite?
Last edited by donkeyscommand (2007-06-03 12:01:14)
Offline
Use the "touching ___" block in the sensors section. Set it either to the specific sprite, or to a colour on that sprite.
Offline
The "broadcast" does not seem to be any more opaque than the other blocks to new programmers. I think that only the programmers who have learned another programming language first find it to be a more difficult concept.
The parallel threading does allow for some sorts of bugs you would not see in serial programs (like race conditions and difficulty in synchronizing activities).
There are some changes I would like to make in the message system:
1) send <message> to <self|sprite|stage|all>
to replace broadcast
2) send <message> with <value>
for passing a parameter
3) wait for <message>
could replace current "when I receive <message>" which is almost the same
as
forever
wait for <message>
do body
The new construct would be safer, as you would not start a script in the middle of running it, and the debugging would be easier.
Last edited by kevin_karplus (2007-06-03 17:03:31)
Offline