Use
of broadcast
A broadcast is
a message that is sent through the control
block
by Broadcast
() and Broadcast
() And Wait,
and are received by When
I Receive ().
Broadcasts
are useful in games and animations. They are used to start an
event, change background or to instruct a script to perform an
action.
Broadcast
() : This block makes
the specified broadcast i.e. a message like “Hello” and has no
further effect.
Broadcast
() and Wait : This
block makes the specified broadcast and freezes its script until
all of the receiving scripts have finished.
When
I Receive () : This
block will stay inactive until it receives the specified broadcast.
Once it has been received, the script goes into action and
ends once it has finished but it can be started more than once.
To
understand these blocks try the following activities:
Activity
1:
To make a bat fly when cat sprite is clicked as cat is instructing
bat to fly.
-
Add sprite bat1-a, and add the costume bat1-b
After
adding costume the screen looks as follows:
-
Add this script to the cat sprite:
On
clicking new type FLY
-
Add this script to the bat sprite:
-
Run the program by clicking on green flag. When the cat is clicked, it sends an invisible message to the bat, the bat receives the message and flaps its wings and flies.
Activity
2: To
change the background when a cat is clicked. When first cat is
clicked a message displayed on stage YOU WIN and when second cat is
clicked stage changed and a message appears GAME OVER.
-
Duplicate the cat sprite on the stage.
-
Add the following script to sprite1.
-
Add the same code to sprite 2 but change the broadcast message to GAME OVER.
-
Click on stage and select background tab. Copy background, edit it and change the colour as blue. Type the text YOU WIN.
Copy
the background again, edit it and change the colour as red with text
GAME
OVER.
Now
the stage settings appears as follows:
-
Add the following script to stage :
-
When green flag is clicked, the screen is:
When
Sprite1
is clicked, the screen looks as:
When
Sprite2 is clicked, the screen looks as:
Note:
-
Broadcasts are sent to all sprites at the same time, but they don't all have to respond
-
You can react to a broadcast from the same sprite that sent it.
No comments:
Post a Comment