Hardware Reference
In-Depth Information
Try using the arrow keys on the keyboard to move the cat sprite towards the Cheesy-Pufs sprite
now. As you can see, when the two sprites meet, nothing happens. hat's because the program
doesn't contain any instructions for what to do when the two sprites overlap—known as a sprite
collision— so it doesn't do anything. his can be rectiied with a new type of block: a Sensing block.
With the Cheesy-Pufs sprite currently active (its image should appear at the top of the
Scripts pane, but if it doesn't, just double-click the sprite on the stage), switch the Blocks
palette into Sensing mode by clicking the Sensing button. From the Sensing palette, drag a
touching ? block into the Scripts pane.
Like the when space key pressed block used to control the cat sprite's motion, the
touching ? block can be customised. Click the down-arrow button next to the question
mark, and choose Sprite1 (the cat sprite) from the list. his block will now be activated when
the two sprites collide.
You can name a sprite by clicking on the box next to its image in the Scripts pane and typing
in a name. Giving the sprites sensible names—such as Cat , CheesyPuffs , and so forth—
makes it signiicantly easier to track what's going on in the program.
TIP
Look at the shape of the touching Sprite1? block. As you can see, it has no jigsaw-like
connectors on the top or the bottom, and it's shaped like a diamond—the same shape used
for a decision point in a lowchart. hat's no accident: the majority of the Sensing blocks
need to be embedded in a Control block in order to operate.
Switch the Blocks pallete to Control mode, and look for the if block—it's shaped like a
squished and bumpy letter C. Note that the if block has a diamond-shaped indentation—
the same shape as the touching Sprite1? block. Drag the if block onto the Scripts pane,
and then drag the touching Sprite1? block onto its diamond-shaped indentation. he
result is a two-coloured block that now reads if touching Sprite1? .
his represents an if conditional in the program: when this point is reached, any code located
within its conines will be executed if and only if the condition is met. In this case, the condi-
tion is that Sprite2 is being touched by Sprite1. With the use of and , or and not logic blocks
from the Operators block palette, some quite complex scenarios can be accommodated.
From the Looks block palette, drag a say Hello! For 2 secs block into the centre of the
if touching Sprite1? conditional. Change the text to read Don't eat me! , and then
add a wait 1 secs Control block, changing the value to 2 . Add a when space key
pressed block to the top, changing the value to read when right arrow key pressed .
Finally, drag a hide block from the Looks palette to the bottom of the loop to end up with
the block list that's shown in Figure 10-9.
 
Search WWH ::




Custom Search