Jump to content

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


AaronG
 Share

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

Link to comment
Share on other sites

  • 1 year later...

[NOTE: sorry for the post resurrection, but we can learn a bit more with it)

Maybe you already solved that, but I will make some observations:

You MUST study about state machines, basically I'm talking about automaton finite deterministic (AFD)

For example, with a AFD, you can control each state of the entity and change when the player or AI changes:

//NOTE: this object.state goes in object as a member, assign its value to IDLE (first state)
function update (  )
{
  switch (object.state)
  {
    case IDLE:
      {
        //the idle code stay here
      }
      break;
    case WALK:
      {
        //the walk code stay here
      }
      break;
    case JUMP:
      {
        //the jump code stay here
      }
      brek;

    default:
      object.state = IDLE;
      break;
  }
}

And so, you must change the object state through the variable member object.state

Think like if each state was independently of each other. So, the IDLE has the own code, WALK has another and JUMP has other.

Each state process the input with diferents ways, see like a code can be:

//NOTE: this object.state goes in object as a member, assign its value to IDLE (first state)
function update (  )
{
  switch (object.state)
  {
    case IDLE:
      {
        //the idle code stay here
        //process inputs in IDLE state
        if (keyPressed == RIGHT || keyPressed == LEFT)
        {
          object.state = WALK;
          break;
        }
        
        //this function check if the object is touching the ground
        if (!object.isGround())//if no
        {
          object.state = FALL;
          break;
        }
      }
      break;
    case WALK:
      {
        if (!object.isGround())
        {
          object.state = FALL;
          break;
        }
        //the walk code stay here
        //process input in WALK state
        if (keyPressed == RIGHT)
          moveToRight();
        else if (keyPressed == LEFT)
          moveToLeft();
        //should be stop
        else
        {
          object.state = IDLE;
          break;
        }
      }
      break;
    case JUMP:
    case FALL:
      {
        //the jump/fall code stay here
        if (keyPressed == RIGHT)
          moveToRightOnAir();
        else if (keyPressed == LEFT)
          moveToLeftOnAir();
        
        //if touchs grounds is idle
        if (object.isGround())
        {
          object.state = IDLE;
          break;
        }
      }
      brek;

    default:
      object.state = IDLE;
      break;
  }
}

In the code above, we can see that each state holds diferents codes, it's more modular, and any state can survive without any other state.

What can I say with all things?

Simple, with state machines your code will stay more readable and easy to expand to more complex code without lost of sense.

Imagine a code with 8K lines, if you follow making the code like you were making, you will finish in problems. If you use the state machine way, your code will stay more easy to mainutence.

 

Now the jump problem. In the code that I show to you, you can verify if the player is jumping over the enemy or just falling above it. Just check the object.state value and you'll see what the player is acting. So in this way, you just need add more code on the JUMP/FALL state part and check if the enemy is down on the player feet. If so, you can die the enemy calling a method inside the own enemy (something like enemy.kill()) to send the message to enemy do the kill()

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...