6.3.4 Inserting Touch Detection

View

The Touching block is a sensing block and a “true or false” block. The block checks if its sprite is touching the mouse-pointer, edge, or another sprite (a reporter block which returns the sprite's name, usually a variable can be used). If the sprite is touching the selected object, the block returns true; if it is not, it returns false. We are not going to provide more details about the touching block since we covered it in module 4. We are going to focus on how we use the touching block in creating games.

  • We are going to create a game which uses variables to calculate the lives and score.
  • In the exercise we are going to use the sensing to effect change in a game.

We will use 2 sprites. One will be called “Cat” and the second will be called “Mouse”. 

Figure 170: Naming a sprite

 

We want the cat to chase the mouse and when it touches the mouse, the cat will say “Thank you God for this food” and the mouse will say “I am dead”. This will be possible when we use the “forever” loop and the “if” condition.

Here is how your code will look like:


Figure 171: Code to let the sprites chase each other on the stage

 

With this code, both sprites will run 100 steps and bounce when they touch the edge. When the cat catches the mouse, the mouse will say “I am dead” and the cat will say, “Thank you God for this food!” Then the game stops. Sensing gives a true or false response. We can then create scripts which do different things if the answer is true or false.

Last modified: Friday, 22 October 2021, 11:26 AM