Do you want to freeze a sprite in Scratch when the player hits an obstacle? You can! It is one line of code, with a combination of three blocks. Quickly and easily, you can add this feature to your game. Please note, this solution works best if arrow keys control the player. If your player is controlled using the mouse pointer, it is not as effective.
Scratch Motion Blocks to Freeze a Sprite
The blocks you need are from the Motions block palette:
Combine the Motion Blocks to Hold a Sprite in Place
When a player touches an obstacle, you need to hold it in place for a certain amount of time. You must take the blocks listed above and put them together. All you need to do is…
- Drag the x position block into the x: circle on the glide block.
- Then the y position block in the y: circle on the glide block.
- Now, set the number of seconds to hold the sprite in place.
The Sprite Bounces Back to its Original Spot
The player AFTER it touches an obstacle, can still press the arrow keys to try to move the sprite. However, it just keeps bouncing back to the x and y position that it was originally at when the two sprites touched. This is how the code appears to “freeze” the sprite. After the correct amount of seconds pass then the player can move to a new location.
Script to Freeze a Sprite When it Touches an Obstacle
Of course, you can’t just have the Motion block combination above in your Scratch program and have it work. You need a script! Now, every game is unique. However, the logic required might be something like this…if the player touches the obstacle freeze the sprite in place for 2 seconds. In Scratch coding this translates into the following script:
Example Game with Freeze Feature
The example below is a game made using Scratch. It is from the TechnoKids PBL project, TechnoRace. This game development curriculum unit has students develop a unique game that has the player race against time. They must avoid obstacles and collect treasure to win.
About the Video: In the game, Space bandits have captured the Prince. The player must avoid the robot to collect the key to set him free. Arrow keys control the player’s movements. Notice that the player freezes when it touches the robot. Since the game has a timer, every second counts!