How to handle instances of same sprite collision / colliding.

Stats

3,627 visits, 5,643 views

Translations

This tutorial hasn't been translated.

Tools

Hi all, this is my first tutorial. It took me a while to figure out how to handle the case of two identical enemies colliding with each other. In my case I wanted to set an enemy on fire and for a few seconds that enemy could set his buddies on fire too.

I found most of what I needed on the forum (http://www.scirra.com/forum/collision-between-instances-of-the-same-object_topic70246.html) specifically vee41's evil sprites demo but didn't see any tutorial so I decided to try and write this one.

This tutorial assumes you know how to use functions (including passing variables), local variables and instance variables.

First create your enemy, like this bush:

Add two instance booleans: OnFire and Flammable. Set Flammable to TRUE and OnFire to FALSE. Add an integer variable FireCounter and set it to 0 (zero).

Add a sprite (in my case, an explosion) and create an event for when it collides with the enemy. In this case it calls the function ("BurnBabyBurn") and passes the UID of the enemy. Note it only sets fire to enemies who are flammable and not already on fire.

Now create an event for when an instance of the enemy collides with another instance of the same enemy. This event uses 2 local variables to hold the UID of the enemies involved. It then checks to see if each enemy is on fire and if so, calls the function ("BurnBabyBurn") to set the OTHER enemy on fire.

Then create the function that handles the burning. When they contact it will set the enemy OnFire only if the enemy is Flammable. It sets the FireCounter to 3 (seconds in our case as you will see below).

Finally create an event that runs every second to kill enemies when they burn out:

Hope this helps and is clear enough to follow!

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!