Sprite Collision and Events

Topic

Sprite Collision and Events

Topic Progress:

In this lesson you will learn how to make sprites collide with each other and how to create events with canvas sprites. A partially complete application has been set up for you with all the sprites pre-loaded and placed.

Click the link below to remix the project and continue with the lesson.

https://x.thunkable.com/projectPage/5ea3be286094e1aa55c50af4

Take a moment to inspect the sprite names, blocks and layout.

The Player sprite will move wherever you tab on the screen, but at this stage will pass through all sprites.

Select the Player_Type and change it’s “Passes Through” property to false

Repeat this for Obstacle_Type’s properties and Wall_Type’s properties.

This allows our Player to collide with Obstacle sprites and Wall sprites, and as such, these sprites can now be pushed around the screen.

Now in Wall_Type’s properties, set the “IsStatic” property to true.

IsStatic means that the sprite will not move unless a code block moves it.

This also means that our Player sprite can no longer push the wall around.

When a sprite has “Passes Through” as false and “IsStatic” as true, it acts like the edge of the screen.

Run the application on your preferred device through Thunkable Live and test out the changes!

Now let’s add some code to our yellow enemy sprite.

We want our Player sprite to be sent back to the start when they touch an Enemy_Type sprite.

To do this, use the “when sprite1 collides with sprite2” we learnt about in the last lesson. 

Now inside that, set our player’s sprite position to the beginning (at X: 80, Y: 340) and set its speed to 0 so that it stays still once it has been moved.

Once you have the following blocks you can continue with the Lesson

Next, we are going to create a new screen when our Player enters the portal which will tell us congratulations.

Create a new screen and add a Label to that screen. 

Remember, it is important to name your components properly, as this helps to create blocks that are easy to understand later.

Give the label an appropriate end screen message and then navigate to our level_1 blocks page.

We want our Player to enter the portal and then have the game change screens.

We do this by setting an event to trigger when the portal sprite (“Exit_Type”) collides with the Player. This is possible by doing something like shown below.

Once you have the navigation working, you can move on to the next lesson.

@

Not recently active