Jump to content

Key Events help


karneekarnay
 Share

Recommended Posts

Hi Guys,

 

I'm pretty new to Phaser Game development. I'm trying to make a cool 2D platformer. I've created a very basic first level, enough to let me start working on the player controls. One thing I want to do is add a double jump feature to my player. I'm trying to find something like a key.up function to use to stop it so my player doesn't just hold down the spacebar to jump really high. Here is my code behind jumps below.

 

    if (userKeys[2].onDown && player.jumpCount < 2)
    {
        player.body.velocity.y = -350*upForce;
        player.jumpCount++;
        console.log(player.jumpCount);
    }

I think what I need is the processKeyUp function, in the keyboard.js. I'm not sure how I call it. Any ideas?

 

Also right now I am using Cloud9 as my development environment. I've heard good things about mighty editor. Which one do you guys prefer?

Link to comment
Share on other sites

Hi Karnee, welcome to the forums.

I'm not a Phaser user so I can't give any advice on framework specific stuff and I'm sure someone more knowledgable in Phaser might give some better advice for you, but, in the meantime...

I'm guessing that `onDown` is a Phaser helper that fires continually while the key is pressed, whereas it sounds like you want a discrete value when the key is intiially pressed. If Phaser exposes such an event then you need to use that instead, the docs should be able to help you (I have my own keyboard input helper lib that emits on initial keyDown, continuously whilst down and on keyUp to handle requirements like yours, Phaser might implement the same).

So once you get the discrete key events (sounds like you might be getting the continuous at the moment) your code is almost there. You'd need some way of resetting that jump count back to 0 when the player lands again.

If you can not get discrete events (which would surprise me), then you can store player state somewhere. i.e. move them into a jump state (which would also update the sprite/ui) and ignore keydown events until they exit that state (no longer jumping i.e. when they land). The bit that gets a little tricky would be registering a keyUp during the jump state so that a subsequent keyDown would initiate the 2ndJump state (uses forces to accelerate your player rather than manually setting velocity might also help here). This logic is a little tricky, which is why I try to stay clear of having to program imperatively like this, although it would be significantly easier if you can get the discrete initial key down event (surely you must be able to).

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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