how to add collision in scratch

How to Add Collision in Scratch: Complete Guide

By Rachel Moore
AD

Scratch is a beloved platform for creative coding, offering endless possibilities for animations, games, and interactive art. Adding collision detection to your Scratch projects is a fundamental skill that brings your projects to life, making objects interact realistically. Here's a comprehensive guide on how to add collision in Scratch.

Before diving into the details, ensure you're starting with a basic understanding of Scratch and have created some simple animations or games. This guide assumes you're already familiar with blocks like "when green flag clicked," "Forever," "if touching," and other essential Scratch syntax.

Understanding Collision in Scratch

Collision detection in Scratch is based on the sprites' x and y coordinates, along with their size (width and height). When two sprites touch, their coordinates overlap, allowing Scratch to recognize them as colliding. To add collision, we'll use the STAGE feature and blocks like "if on edge bounce" and "touching."

AD
Catch the Falling Fruit Game in Scratch | Easy Scratch Game Tutorial by Kodex Academy
Catch the Falling Fruit Game in Scratch | Easy Scratch Game Tutorial by Kodex Academy
How to Make a Platformer with Ground Detection in Scratch | Platformer Tutorial Part 2
How to Make a Platformer with Ground Detection in Scratch | Platformer Tutorial Part 2
Build a Space Invaders Game in Scratch 3.0 | Free Coding Tutorial ⭐
Build a Space Invaders Game in Scratch 3.0 | Free Coding Tutorial ⭐

First, let's understand the STAGE. In Scratch, the STAGE represents the background where your sprites interact. It's crucial for collision detection as it sets the boundaries for your sprites. Ensure your sprites don't move beyond the STAGE's edges by using "if on edge bounce".

Setting Up the Stage

To begin, click on the "Stage" option in the Sprite list, represented by a yellow screwdriver. The Stage Properties panel appears on the right. Here, you can set the stage's width and height, and choose if your stage is transparent (allowing the web page background to show through) or not.

an image of a computer screen with the words, symbols and numbers in thai language
an image of a computer screen with the words, symbols and numbers in thai language
Scratch Cutscene Tutorial for Kids – Story Dialogues Made Easy
Scratch Cutscene Tutorial for Kids – Story Dialogues Made Easy
the worksheet is shown for children to learn how to read and use it
the worksheet is shown for children to learn how to read and use it

Set your stage's width and height according to your project's needs. A common ratio is 640x480 pixels, but you can adjust it based on your animations. Ensure your sprites don't exceed the stage's bounds by using "if on edge bounce" to stop or bounce them off the stage borders.

Creating Sprites for Collision

Add your desired sprites to the Stage. To add collision detection, select "touching" from the Events menu. This event triggers when the selected sprite touches another sprite or the stage boundaries. For example, to make a sprite stop or change its direction when touching another sprite, add a "touching" block and connect it to the desired action, like "stop" or "put pointer in position 50 -200 (make a new sprite or object, or use a built-in one like the Cat or Alien).

AD

Let's say you want your cat sprite to stop moving when it touches the alien sprite. First, add a "when green flag clicked" block to the script area for the cat sprite. Next, attach a "touching" block with the alien sprite selected. Then, connect a "stop" block to make the cat sprite stop moving when it touches the alien.

an assortment of different colored plastic objects with numbers and symbols on them, all in the same
an assortment of different colored plastic objects with numbers and symbols on them, all in the same
How to Make a Zombie Survival Game in Scratch!!!
How to Make a Zombie Survival Game in Scratch!!!
Scratch Collision Detection: Halloween Conditional Coding Activity - Our Family Code
Scratch Collision Detection: Halloween Conditional Coding Activity - Our Family Code
How to Make Many Bouncing Balls in Scratch
How to Make Many Bouncing Balls in Scratch
Scratch Code
Scratch Code
How to Make a Geometry Dash Game on Scratch - Create & Learn
How to Make a Geometry Dash Game on Scratch - Create & Learn

Advanced Collision Detection

Sometimes, basic "touching" collisions aren't enough. You may want to differentiate between sprites, prevent a sprite from touching the stage edges, or create more complex interactions. For these cases, you can use the "touching edge" and "touching color" blocks along with some creativity.

For instance, you can use "touching edge" to ensure a sprite bounces off the stage borders without using the STAGE properties. Just add a "when green flag clicked" block and connect it to "if on edge bounce". This makes the sprite bounce off the stage edges but won't stop or change its direction when touching another sprite.

AD

Handling Complex Interactions

Sometimes, you may want sprites to react differently based on what they touch. The "touching color" block is useful here as it allows you to create scripts that react to different colors. For example, you can use it to make a sprite change color or move differently based on which sprite it touches.

✨ scratch✨
✨ scratch✨
Tricky motion blocks scripts – so similar, yet so different!
Tricky motion blocks scripts – so similar, yet so different!
El Proyecto Scratch de los Viernes (XXVIII): los números aleatorios
El Proyecto Scratch de los Viernes (XXVIII): los números aleatorios

Here's how to implement it: First, decide which sprites you want to differentiate. Then, use the "touching color" block with the desired color selected. For example, if you want your cat sprite to turn green when it touches a blue alien but not change color when it touches a red alien, you'd use the "touching color (blue)" block and connect it to the "change color by" block to turn the cat green.

Creating Custom Collision Events

Scratch scripts can become complex as your projects grow, making it challenging to manage multiple collision events. To handle this, you can create custom events by adding "define" blocks at the beginning of your script. These help keep your scripts organized and make them easier to modify.

AD

To define a custom event, simply add a "define" block to the top of your script, followed by a "when I receive" block with the desired event name. For example, if you want to create a custom event called "collides with alien", you'd add a "define" block with "when I receive collides with alien". You can then add the code to trigger this event based on your needs.

Experimenting with different combinations of blocks and sprites makes adding collision in Scratch a rewarding challenge. Remember that the STAGE and "touching" blocks are the foundation of collision detection, and built-in or custom events help manage complex interactions. Keep practicing, and your Scratch projects will come to life with realistic collisions!

AD