DWboutin Posted December 22, 2013 Share Posted December 22, 2013 Hi, I want to make my character jump, but stop the acceleration in Y when i release the up key. How can i do this? (sorry for the noob question) Thank you ylluminarious 1 Link to comment Share on other sites More sharing options...
Karma Octopus Posted December 22, 2013 Share Posted December 22, 2013 This tutorial shows you how to make a character jump in a simple way. http://www.photonstorm.com/phaser/tutorial-making-your-first-phaser-game Link to comment Share on other sites More sharing options...
DWboutin Posted December 22, 2013 Author Share Posted December 22, 2013 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 More sharing options...
XekeDeath Posted December 22, 2013 Share Posted December 22, 2013 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 More sharing options...
DWboutin Posted December 22, 2013 Author Share Posted December 22, 2013 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 More sharing options...
DWboutin Posted December 22, 2013 Author Share Posted December 22, 2013 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 More sharing options...
rich Posted December 23, 2013 Share Posted December 23, 2013 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 More sharing options...
DWboutin Posted December 23, 2013 Author Share Posted December 23, 2013 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 More sharing options...
rich Posted December 23, 2013 Share Posted December 23, 2013 Describe what actual motion you're trying to get the player to achieve, because I don't really understand it from your code. Are you after a Mario style jump? I.e. press once, up he jumps, gravity pulls him down? Or something else? Link to comment Share on other sites More sharing options...
DWboutin Posted December 23, 2013 Author Share Posted December 23, 2013 More a super meat boy jump style. Longer you pressed into the JUMP button, more he jump higher (to a maximum height). If you press just a little, he will do a small height jump.Thank you for your help Link to comment Share on other sites More sharing options...
@99golems Posted December 23, 2013 Share Posted December 23, 2013 -deleted: misunderstood the question- Link to comment Share on other sites More sharing options...
DWboutin Posted December 23, 2013 Author Share Posted December 23, 2013 Deleted (answer for the deleted above comment) Link to comment Share on other sites More sharing options...
@99golems Posted December 23, 2013 Share Posted December 23, 2013 Sorry then! I think i misunderstood the question. Link to comment Share on other sites More sharing options...
rich Posted December 24, 2013 Share Posted December 24, 2013 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 More sharing options...
DWboutin Posted December 25, 2013 Author Share Posted December 25, 2013 Thank you Rich! I just replaced my cursors = game.input.keyboard.createCursorKeys();With addKey and check if jumpKey isUp! Have a beautifull christmas! Link to comment Share on other sites More sharing options...
Recommended Posts