Learn how sprites can 'sense' things like touching a color or the mouse pointer.
How does a video game character know they've hit a wall or collected a gold coin? They don't have eyes like we do, but they have something even cooler: Sensing Blocks!
In coding, Sensing is how a sprite gets information about its world. Think of it like a cat's whiskers or your own sense of touch. Without sensing, a sprite would just walk right through walls or ignore the player! The most common sensing block is the Touching? block. It asks a question: 'Am I touching something right now?' Because this is a question, it usually fits inside an If-Then block. If the answer is 'Yes' (or True), the sprite will run the code inside. If the answer is 'No' (or False), it just keeps going.
Let's make a ball stop when it hits the edge of the screen. 1. Drag a 'Move 10 steps' block inside a 'Forever' loop. 2. Place an 'If-Then' block inside the loop. 3. Put the 'Touching [edge]?' sensing block into the 'If' slot. 4. Inside the 'Then' space, put a 'Say [Ouch!] for 1 second' block. Now, every time the ball hits the edge, it will speak!
Quick Check
Why do we put the 'If-Then' block inside a 'Forever' loop?
Answer
So the sprite never stops checking if it is touching something. Without the loop, it would only check once at the very start!
Sprites don't just sense the edge; they can sense each other! This is how you create a 'tag' game or a 'collection' game. When you use the Touching [Sprite Name]? block, you are telling the computer to watch for the exact moment two sprites overlap. Imagine Sprite A is a cat and Sprite B is a mouse. We can tell the cat: 'If touching mouse, then play a meow sound!' This turns a simple drawing into an interactive game.
Create a script for a 'Coin' sprite to disappear when a 'Player' sprite touches it. 1. Start with a 'When Green Flag Clicked' block. 2. Add a 'Show' block so the coin appears at the start. 3. Add a 'Forever' loop. 4. Inside, add an 'If Touching [Player]?' block. 5. Inside the 'If', put a 'Hide' block. 6. Now, when the player 'touches' the coin, it vanishes!
Quick Check
If Sprite A is touching Sprite B, is Sprite B also touching Sprite A?
Answer
Yes! Sensing works both ways. Both sprites can detect the touch at the same time.
Sensing also lets the sprite 'feel' the human player. The Touching [Mouse-pointer]? block is a powerful tool. It allows you to create buttons that glow when you hover over them or characters that follow your mouse. You can even use the Ask and Wait block to let the user type in an answer. This makes the program feel alive because it is reacting to Input—information given to the computer by the user.
Make a button change size when the mouse touches it. 1. Use a 'Forever' loop. 2. Inside, use an 'If-Then-Else' block (this has two paths!). 3. In the 'If' slot, put 'Touching [mouse-pointer]?'. 4. In the 'Then' path, set size to . 5. In the 'Else' path, set size to . Now the button grows when you point at it and shrinks when you move away!
Which block is most likely to hold a sensing block?
What happens if you use a sensing block WITHOUT a forever loop?
A sprite can sense if it is touching a specific color on the background.
Review Tomorrow
Tomorrow, try to explain to a friend why a 'Forever' loop is the 'battery' that keeps sensing blocks working.
Practice Activity
Try this on your own: Create a 'Lava' game where a sprite has to move across the screen but 'dies' (hides) if it touches the color red!