Sign in to follow this  
AaronG

JavaScript Platform Game how to destroy enemys by jumping on them.

Recommended Posts

What I want to do is make it so if the player touches the enemy from the side then the player either loses a life and or it causes the game to be over. 
But if the player jumps on top of the enemy then it causes that enemy to disappear.


If a the player touches an enemy
then the player loses a life
which is done by running this code...

    this.status = "lost";
        this.finishDelay = 1;


How ever if the player jumps on the enemy 
then the enemy dies
which is done by running this code...

    this.actors = this.actors.filter(function(other) {
          return other != actor;
        });

Here is my code so far...

   Level.prototype.playerTouched = function(type, actor) {
      } else if (type == "lava" && this.status == null && player.y == enemy.y) {
        this.status = "lost";
        this.finishDelay = 1;
      } else if (type == "coin" && player.y > enemy.y) {
        this.actors = this.actors.filter(function(other) {
          return other != actor;
        });
      }
    };
    
The part that I am having trouble with is...

when the player touches the enemy while the player is on the ground
(the players y axis is equal to the enemys y axis)...

player.y == enemy.y

and

when the player jumps on top of the enemy.
(the players y axis is greater then the enemys y axis)...

player.y > enemy.y
 

My total code is posted as an attachment

platformgame2.html

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.