FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Learn JavaScript with Phaser

Game Mechanic: Jumping on Enemies

The game mechanic of jumping on enemies to get rid of them has been made very popular by Mario games. It is a great way to use the jumping dynamic in a creative way.

We can use the same technique as many Mario games use to do this. An enemy will kill our player unless our player is falling when they make contact.

 

Check the Code: what we need to know and do

Our mechanic relies on us being able to check if we are jumping on our enemy. We will do this by checking to see if our player is on the way up or coming down from a jump. To do this we'll need to know about the following game elements;

The code for a minimal example of the Jumping on Enemies game mechanic is shown here -  https://game-mechanic-jump-on-enemies.glitch.me/

Going over the code:

Have a look at an updated hitHazard function for the code that checks to see;

var hitHazard = function (player, hazard) {
    if (hazard.type === "enemy" && player.body.velocity.y > 0){
      hazard.kill();
    }
    else {
      player.kill();
    }
};

While it was not really needed for this minimal example, there this function makes distinction between this enemy and other possible different hazard types that might be in the group.

To make this work this involved adding the following line of code when creating our enemy in the create function.

    enemy1.type = "enemy";

That's it. We hope you enjoy adding this game dynamic to your game to jump on enemies.

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.