jorbascrumps Posted February 18, 2018 Share Posted February 18, 2018 I'm working on a simple platformer in my spare time with Phaser 3 and I'm currently working on trying to tighten up my controls. Currently I'm trying to prevent jump spamming so that every jump is an intentional action taken by the player. One press of the jump button should translate to only one jump being performed (ie, key must be released to jump again). Looking at the `Key` class, each key has a property of interest: `_justDown`. This appears to be the property I should use, however it's always `true`. Digging into the source code for v3.1.0 the only time this property is modified is during the process event (`ProcessKeyDown`) and when manually checking using `JustDown`: function create () { this.cursors = this.input.keyboard.createCursorKeys(); } function update () { if (Phaser.Input.Keyboard.JustDown(this.cursors.up)) { // Do some jumping } } This seems to work--sort of--except `ProcessKeyDown` gets called repeatedly and resets the property.. Is there an alternate approach I should be using? I know I could use a jump timer but that seems like a hack. Perhaps this scenario is simply a bug with Phaser at the moment? Link to comment Share on other sites More sharing options...
nkholski Posted February 18, 2018 Share Posted February 18, 2018 Instead of justdown I would do something like this with the first statement the beginning of the update loop. It's also easy to expand to support variable height (higher jumps with longer press time): // The player might try to jump if the jump key has been released while standing on the ground if(!keys.jump.isDown && player.body.blocked.down){ player.allowedToJump = true; } // The jump key is down, the body is on the ground and the player is allowed to jump => jump! if(keys.jump.isDown && player.body.blocked.down && player.allowedToJump){ /* Insert jump code */ player.allowedToJump = false; } Link to comment Share on other sites More sharing options...
jorbascrumps Posted February 19, 2018 Author Share Posted February 19, 2018 Thanks for the suggestion, @nkholski. This may be the route to go until `_justDown` is working correctly. Link to comment Share on other sites More sharing options...
jorbascrumps Posted February 20, 2018 Author Share Posted February 20, 2018 @nkholski I just wanted to thank you again for this suggestion. I just finished implementing it in my game and it'll do the trick for now. nkholski 1 Link to comment Share on other sites More sharing options...
nkholski Posted February 21, 2018 Share Posted February 21, 2018 Glad I could help! I actually prefer that approach over just pressed because it's easier to implement variable jump height depending on how long you hold the jump key. Link to comment Share on other sites More sharing options...
Recommended Posts