Synchronizing with broadcasts

Capture the Sprite

Figure 5 shows the main script for the Ball sprite. I'm not showing all the initialization scripts for the game, but when the sprite receives the Capture Sprite broadcast, the first action is to show the Ball, which implies it's hidden at the start of the game while the player is walking.

The repeat() loop is currently set to 1, which gives the player one chance to catch the sprite or it escapes. You can certainly adjust this value.

To throw the Ball toward the sprite (or more accurately, toward the center of the stage) click the left mouse button. The wait until(mouse down?) block pauses the script until it detects the button click. The next block waits until the button is released (not pressed) before the ball moves. The wait until() block works by stopping the remainder of the script from running until the specified condition is true.

When the player throws the Ball, it moves up the screen via the change y by (8) block until the sprite passes the y position of the sprite. As the ball moves toward the sprite, I make it get progressively smaller. Like displaying the sprite, this gives the Ball an appearance of traveling away from the player.

The script ends by evaluating whether or not you hit the sprite with the ball. You score each time you hit the sprite. And if you miss the sprite too many times, it will escape. As I mentioned, my game play in Figure 5 gives the player one chance to capture the Ball. After the player misses or captures the sprite, the script sends another broadcast titled Continue Hunting.

Continue Searching for Sprites

The Continue Hunting scripts initialize the starting values for the Player, sprite, and Ball sprites so that the player can continue walking and searching for more sprites. If you've created the game to this point and have played it, you will notice that after a sprite is captured or escapes, the next sprite never displays until the player changes to a new background.

That's because the sprite script seen in Figure 4 is only called in two places. I mentioned the first place already: when the backgrounds change. The other place is a broadcast(Control Sprite) block at the start of the game, which will make a sprite appear on the first background.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content