In last tutorial, we created an animation of a flying bat. In this tutorial we will learn how two sprites can communicate between each other.
Each sprite has its own scripting area, where the scripts are written perticularly for the parent sprite. For example in the last tutorial you had only 1 sprite, thus 1 scripting area and the script was created for bat.
When you want to have one sprite do certain action only when the other sprite commands, you have to use broadcast protocol. In this protocol, one sprite broadcast message and other sprite start a script only when the message is received.
- Broadcast Message: block is a Event block and a stack block which sends a broadcast throughout the whole Scratch program. Any scripts in any sprites that are hatted with the When I Receive () block that is set to a specified broadcast will activate. This broadcast block lets scripts send broadcasts without any waits in its script.
- The Broadcast () and Wait block is a Event block and a Stack block. The block sends a broadcast throughout the whole Scratch project — any scripts that are halted with the When I Receive () block and are set to the broadcast will activate. This broadcast block lets scripts send broadcasts, and have them wait until all scripts activated by the broadcast end.
- The When I Receive () block is an Events block and a Hat block. Scripts that begin with this block will be invoked once the specified broadcast has been sent by a calling script.
In this project, there are two sprites, one cat and one bat flying. Whenever you press ‘Space’ button, the cat will say “Fly Bat”, then the bat will reply “Ok Cat” and will pick a random location in the sky and fly there.
- First you have to setup the stage. Select stage and import ‘Brick Wall 1’ from the Background library form file menu.
- In the new sprite panel, choose the “Upload sprite from the file and change its name to “Cat” by clicking on the sprite. Go to costumes, select the shrink option above the paining board and click on cat to reduce its size. You can drag the cat on the stage and place it on ground.
- Choose the bat sprite from the library. Place the bat above ground as shown in figure.
- Go to Script tab. This time we will start the program with a different hat block. We will use when green flag is pressed. This green flag is above the stage on the right side. So, when you click it the bat will start flying. Given below is the script to make the bat fly.
When the program starts, the bat will first go to the coordinate (-150, 80) (where X coordinate is -150 units and Y coordinate is 80 units) using glide block from Motion tab. Then the bat will start flying, To make the animation we will change its costume after every 0.5 seconds forever.
- Now you have to make the cat send a message to bat when you press ‘Space’. So, what this basically means that, when you press ‘Start’, a script of a cat will start executing and will send a message to another script of the bat which will start executing. In the Control tab, you can find ‘When I receive’ block which is hat block and start execution whenever you broadcast a message in another script, which you can find below it. Hence, the cat will broadcast a message and the bat will receive it.
- Cat broadcast script: The cat script is simple and it is recommended to use ‘say’ block for other users, so that they know is going in the animation. You can find it in Looks tab.
- Bat Script: When the bat receives a message from the cat it has to go to a random position on the X axis. Use ‘Pick random’ block to get a random number between -150 to 150, and glide to that location using glide block.
Now, you project is ready and you start executing it using green flag. Tune the time and wait blocks for getting the best animation.