arrow_back Catch a Break
In this video, you'll add a sprite that bounces around the screen and makes a special offer to the audience watching your commercial when a user clicks on it.
First, add any sprite to your project.
To make the sprite bounce all over the screen, add a "goto" block and two "pick random" operator blocks with the height and width dimensions of the stage as the X and Y values.
Try that out.
Each time the block is clicked, the sprite hops from one spot to another on the screen.
Add "repeat" and "wait" blocks to make the sprite jump around the stage many times, pausing between each jump to give the user a chance to click it.
Tinker with the values in the "repeat" and "wait" blocks until you like the way the sprite moves.
The higher the number in the repeat loop, the more times the sprite will move around the screen.
The higher the number in the wait loop, the longer the sprite will pause in one place.
Higher numbers will make it easier for your audience to click on the sprite.
Lower numbers can make the sprite hard to catch.
Remember to try your code often.
That looks great!
Drag out a "say for 2 seconds" block, and enter the instructions the sprite should say when it first appears on the screen.
This example says "Catch me for a free gift!"
But, you can enter any text you like.
If the sprite stays in one place while it gives the instructions, it will be very easy for an audience member to click on it.
Instead, the sprite should move around the screen at the same time as it says the instructions.
Test this out by clicking on the "say" block, then quickly clicking on the "repeat" loop.
To make both the "say" and the "go to" actions happen at the same time, drag out a "when I receive" event.
Click the dropdown arrow, choose "new message," and add a message name you like.
This example calls the message "Catch me."
Snap this "receive" block above the repeat loop.
Drag out another "when I receive" block.
Place it above the "say" block, and choose your message name from the dropdown.
Both stacks of code are now waiting to receive the same cue, or event, that tells them it is time to act in the commercial.
To test the code, drag a "broadcast" block into the script area, and choose the message you just added from the dropdown list.
Test your code by clicking the “broadcast” block.
In this example, the event name "Catch me" is broadcasted to all the sprites in the project.
The penguin is waiting to receive the event.
It immediately starts running all the code under any "When I receive Catch Me" blocks.
The penguin simultaneously runs the “say” block and bounces around the screen.
That looks great!
Next, add code for what happens if the user successfully clicks on the sprite.
Add a "when sprite clicked" event and a "say" block to reveal what the user won.
Test the code by clicking the "broadcast" block again, then the sprite...
if you can catch it!
That looks great!
To make the sprite stop moving when clicked and also to hide it before and after the event is called, add a "stop other scripts in sprite" block and some "show" and "hide" blocks.
Try the code by clicking the "broadcast" block.
The sprite gives some instructions, moves around the screen, and reacts when clicked.
However, the most important step is still left for you to do.
If you don't add the "broadcast" block to the code you already built in another sprite, then none of this new code will ever run during the actual commercial!
This example will add the the "broadcast" block to the Scratch Cat, but you should add it where you like for your project.
If you drag the broadcast block under the "when flag clicked" event and set the broadcast block to the "catch me!"
event, for example, the penguin will display at the very beginning of the commercial.
You can even use the event more than once.
Adding the "catch me" event near the end of the code will make the penguin reappear and say the instructions a second time.
Remember, your code will look different than the example you see here, but it is important that you add your new broadcast event somewhere in your other sprite's code.
Otherwise, the new code you created will never run and your users will never have a chance to catch your sprite!
Now it's your turn.
Add a new sprite.
Code the sprite to give instructions, move around the stage, and react when the user clicks on it.
Remember to add the "broadcast" block to another block stack so that your new code is run during your commercial.
The "broadcast" block needs a matching "when I receive broadcast" block to run.