Final answer:
To make a sprite turn in the opposite direction when it touches the edge of the stage in Scratch, use the 'If on edge, bounce' block inside a forever loop. This script is common in Scratch games and animations to keep sprites within the stage's boundaries.
Step-by-step explanation:
The code that turns a sprite in the opposite direction when the sprite touches the edge of the stage in Scratch is a simple script. You can use a combination of the If on edge, bounce block along with motion blocks to achieve this behavior. The If on edge, bounce block automatically detects if the sprite is touching the edge and turns the sprite to head in the opposite direction.
Here is a step-by-step explanation on how to create the script in Scratch:
- Drag and drop an if on edge, bounce block from the Motion category into your script area.
- Place this block inside a forever loop to constantly check for the sprite touching the edges.
- As soon as the sprite touches the edge of the stage, the block will execute and the sprite will turn in a direction that simulates bouncing off the edge.
This script is usually utilized in games and animations on Scratch to prevent sprites from moving outside the visible area of the stage.