Platform-style crushing blocks

3

Attached Files

The following files have been attached to this tutorial:

.capx

crusher-platforms.capx

Download now 7.43 KB

Stats

3,532 visits, 5,392 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 3 Jul, 2013. Last updated 19 Feb, 2019

From the documentation on the Solid behaviour @ scirra.com/manual/104/solid:

"The behaviors which respond to the Solid behavior usually get stuck if Solid objects crush or otherwise trap the object deep inside the Solid object. [...] it is up to you to design your games in such a way that the player cannot be crushed or trapped by moving Solid objects. You should be especially careful when moving Solids up against other Solids."

Are you kidding?! Getting crushed by solids is half the fun of platform games! (think Super Mario World underworld and castle levels). Luckily, we can exploit this problem of "getting stuck" to add crusher blocks with relative ease.

The general approach I've come up with is to overlay an invisible sprite (in my case I call it a "DeathSensor") on any solids in which the player may get stuck--this will either be the moving block itself, or the ceiling or walls with which it makes contact. (I've not encountered a scenario where the player gets stuck in the floor so I've not built any conditions to test for this.)

With these invisible blocks in place it's then simply a matter of checking if the player is colliding with them. Because the "DeathSensors" are inside the solids there shouldn't technically be a collision unless the player is stuck (in reality it's not quite that simple, but more on that later).

Let's have a look at the example:

Above you can see my object types and general level layout before adding the crushers. Note that I have a separate (invisible) sprite to re-spawn the player if they get crushed or fall in the pit. The reddish rectangles are my "DeathSensors". I've made them 50% opaque so you can see what's going on but in a real game you would want these to be invisible.

In the next image you can see the placement of my crusher blocks (the "Crusher" objects). I'm using the Sine behaviour to move them 32 pixels in either direction (a magnitude of 64). I've created a separate layer for organization but there's no reason they couldn't be on the same layer as everything else.

Notice I've initially placed a couple DeathSensors manually. But what if the player gets crushed from the side or above? Let's have a look at the event sheet:

* on start of layout I spawn a new DeathSensor for every Crusher. Note that my Crusher object has an instance variable called DeathSensorID--this is used to store the UID of the DeathSensor we just spawned.

* for every tick I loop over all Crushers and look up the corresponding DeathSensor by its UID. If it's on-screen I then proceed to move it to the position of its corresponding Crusher (no point in moving it it's off-screen)

Why the SENSOR_THRESHOLD global variable? Well it turns out that when two objects are moving towards each other occasionally the DeathSensor may register a collision. SENSOR_THRESHOLD is used to apply a padding (in pixels) so that the collision doesn't trigger until the player is "embedded" deeper in the solid (in the case of my examples, 4px deeper).

I've not run into this with any of my static DeathSensors yet, but you might depending on your player's maximum velocity or the speed of your moving solids. If you do find that the collisions trigger unexpectedly then it'd simply be a matter of offsetting the static DeathSensors a few pixels, or checking if the player "Is overlapping at offset" and applying a threshold in that manner.

(Alternatively you could tweak the collision polygon of the DeathSensor object to apply a padding at that level, however, keep in mind that the collision polygon is scaled up and down along with the object, and therefore so is any padding you've added.)

Last but not least: when the player collides with a DeathSensor I simply move them to the spawn point. Here you can see the project in action:

That's really all there is to it! Have fun with it and do let me know if you have run into any issues or have any improvements to suggest.

.CAPX

crusher-platforms.capx

Download now 7.43 KB
  • 0 Comments

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