Jump to content

On key up event or on release


DWboutin
 Share

Recommended Posts

That's the tutorial i'm beggining with, but it don't show how to stop the height acceleration when my up key is released.

 

I'm seeking for the best usage to do this. I don't want to call a function until my character touch the ground, but a function who stop the acceleration when i release the jump (to jump less higher). 

Link to comment
Share on other sites

Take a look at the events.js file in the gameobjects folder, and you will see all the events you can attach a callback to.

One of them is onInputUp, which is what you are looking for.

 

Edit: Now that I have read the tutorial, you can also do it the exact same way you stop the sideways movement.

Link to comment
Share on other sites

Sorry i just began with Phaser, but i want to know how to use it perfectly.
 

I got this for now, but it won't work on the jump velocity.

game.input.keyboard.onUpCallback = function( e ){            if(e.keyCode == Phaser.Keyboard.UP){                console.log(player.body.velocity.y);                player.body.velocity.y = player.body.velocity.y;            }        };

Is this a good way to handle the onUp event, and how can i stop the velocity acceleration?

 

Thank you guys

Link to comment
Share on other sites

I just tried this. But i don't like the deacceleration. How can i set it smoother?

 

game.input.keyboard.onUpCallback = function( e ){            if(e.keyCode == Phaser.Keyboard.UP){                if(player.body.velocity.y < 0){                    player.body.velocity.y = 0;                }            }        };

Edit: i set it to, it's smoother, but it must have a way to get it smoother?

if(player.body.velocity.y < -50){                    player.body.velocity.y = -50;                }
Link to comment
Share on other sites

In the tutorial the vertical velocity is just set once, it's not continuously applied if they hold the jump key down because of the 'touching floor' conditional check.

 

I wouldn't use a callback in the way you've done, it's much easier to create a Key object:

var jumpKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);

Then you can poll the new jumpKey object like so:

player.body.velocity.y = 0;if (jumpKey.isDown) {  player.body.velocity.y = -200;}

Doing the above will apply -200 velocity for as long as the key is held down. If they let go velocity will return to 0 (and if gravity is applied, they'll fall back down again).

Link to comment
Share on other sites

Thank you Rich, but how do i organise this code? If i put 

player.body.velocity.y = 0;

in my update function, my character fall very very slowly. There's what i got in my create function (i followed your tutorial) to handle the jump key.

cursors = game.input.keyboard.createCursorKeys();

And this is my jump handling in my update function. Maybe you'll understand my problem. For now it works well, but if you think it have a better way than calling the onUpCallback, i'll follow your advices.

if (cursors.up.isDown && player.body.touching.down && jumpReleased)        {            jumpReleased = false;            player.body.velocity.y = -400;        }        game.input.keyboard.onUpCallback = function( e ){            if(e.keyCode == Phaser.Keyboard.UP){                if(player.body.velocity.y < 0){                    player.body.velocity.y = 0;                }                jumpReleased = true;            }        };
Link to comment
Share on other sites

I'm not going to give code for this, but will explain how Phaser works so you can get a better understanding of what to apply:

 

First of all use the Key objects in the way I showed above, not using an in-lined update function.

 

Velocity is constantly applied to the body. A velocity of zero doesn't move. A velocity of 100 moves 100px^2 every second. There's no acceleration or deceleration.

 

If a body has gravity or drag applied, it effects the amount of velocity that gets applied to the body.

 

If you want a Sprite to jump just once, and only when they are touching something on their bottom face, then when the jump key is pressed give the body a -y velocity of several hundred - if you ensure that you've given the body gravity in your set-up then this will cause the Sprite to jump and then be dragged back down by the gravity. You'll want to keep track of when they last pressed the jump button too (the Key object has this property), that way you can stop them spamming the jump button, allow them to double-jump in the air (i.e. when not touching the ground) and all sorts of things. Also most platforms have a 'freefall' jump allowance, i.e. if the Sprite is touching the ground, but then falls off a ledge, for those first few ms most games still let you jump (even though you're technically no longer touching the ground).

 

Ultimately what you want to achieve is specific to the sort of game you're making, but I'm pretty sure with a mixture of body gravity / bounce / velocity and a Key object, you can do it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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