2
\$\begingroup\$

I'm trying to make a Megaman-style wall jump in my 2D platform game with JavaScript.

The character can do A,B,C motions when it sticks the wall:

enter image description here My problem is that when I trying to jump off the wall(motion C), it goes to motion A or B first, C was not happened. I think the keyboard input was conflict but I have no idea how to handle it.

Here is my current code:

if(key['right'] && key['jump']) {//it doesn't work
    //jump off the wall
} else if(key['right']) {
    //leave the wall
} else if(key['left']) {
    //stick the wall
} else {//release all keys
    //falling down
}

Please, Any help would be appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ Have you confirmed whether the jump input is ever received, just maybe a frame or two before or a frame or two after the rightward input, so that they weren't both received at the same moment? If the input is coming in, just at the wrong time (humans being sloppy input sources, as they are) then we can solve it with some buffering or windows of opportunity on actions. But if the jump input never arrives at all when you're holding right, then it might mean you have a keyboard ghosting issue and need to remap your controls. \$\endgroup\$
    – DMGregory
    Jul 10, 2017 at 4:31
  • \$\begingroup\$ @DMGregory I think the problem is not a keyboard ghosting, the code above key['right'] && key['jump'] can be input right->jump or jump->right. So it had a chance to execute else if statement in game loop. It is my first time to handle inputs at the same moment, so I have no idea how to fix it. \$\endgroup\$ Jul 10, 2017 at 5:04

1 Answer 1

3
\$\begingroup\$

Your actual way of handling this is a frame perfect, this mean that to actually go to the C state you have to press both right and jump at the same frame (which very hard). A solution could be to implement a timer and an additionnal boolean. You have one boolean isOnTheWall and when you press the key['right'], you start the timer. If after X time the key['jump'] has not been pressed you go to state B. If the jump button is pressed then you go to state C. Here is the pseudo code :

if(isOnTheWall && key['right']){
    time++;
    if(time > maxTime){
        time = 0;
        isOnTheWall = false;
        //leave the wall;
    }
    else if(key['jump']){
        time = 0;
        isOnTheWall = false;
        //jump off the wall
    }
    ...
}

Note that time could be replaced by frames depending on what you are trying to achieve.

EDIT : Here are two other examples of how it could have been achieved :

  • You could check for the key['jump'] before the right key so that you don't have to have any timer at all. If your character is on the wall and you press jump (without pressing any direction), your character would automatically jump to the opposite side of the wall and then you could press any direction to adjust the jump. This is a common solution but the feeling is less intuitive and precise to my mind.
  • You could check for the distance between the wall and your character instead of a timer. If the distance is lower than X, you can make still jump to the other direction. Using this method you won't have to wait for your character to leave the wall.

Anyways, I would still chose the first option which feels better when playing.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you very much, it works good! But I still have a question, in this solution, if press key['right'], the character can't move immediately, maybe a little delay until achieve maxTime. Is it the common solution in game development? \$\endgroup\$ Jul 10, 2017 at 10:57
  • \$\begingroup\$ Glad it works ! I know some game developpers use this solution (for instance in Super Meat Boy or Gish) and I assume this is pretty common. I'll update my answer to include other examples of how it could have been made. \$\endgroup\$
    – Shashimee
    Jul 10, 2017 at 12:02
  • \$\begingroup\$ Great answer, thanks! I think I can take a lot from your experiences. \$\endgroup\$ Jul 10, 2017 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .