jd.joshuadavison Posted May 10, 2018 Share Posted May 10, 2018 Hi, Broad question It's my second day learning Phaser. I've dived straight into Phaser 3, but its proving challenging since the docs are only available in the GitHub repo. It looks like there was an event handler in Phaser 2 called 'onUp' which would only fire once when a key was released instead of continuously firing for as long as the key is held down like 'isDown' does. This doesn't seem to exist in Phaser 3 - what is the correct way of doing this in Phaser 3? In this particular instance I'm trying to do this on a cursor key, but it would be useful to know how to do it on any key if it's different. Specific scenario I've enabled my player sprite to 'double jump' using the following code in the update function. if (cursors.up.isUp) { jumpReleased = true; } if (cursors.up.isDown && player.body.onFloor()) { player.body.setVelocityY(-300); player.anims.play('idle', true); jumpCount = 1; jumpReleased = false; jumpTime = new Date().getTime(); } else if (cursors.up.isDown && jumpReleased === true && jumpCount === 1 && ((new Date().getTime() - jumpTime) < 1500)) { player.body.setVelocityY(-400); player.anims.play('idle', true); jumpCount = 2; jumpReleased = false; } `isDown` simply fires if the key is being pressed at the time that the update function fires, which is a lot. I was hoping to find a way to simply fire this event on key up (like the native javascript event handler) - this way I wouldn't have to set and check `jumpReleased`. Is there a way to do this? More to the point, is there a correct way to do this? Thanks, Josh Link to comment Share on other sites More sharing options...
Doug Posted May 11, 2018 Share Posted May 11, 2018 Hi Josh I think you're looking for this: http://labs.phaser.io/edit.html?src=src\input\keyboard\keydown.js You just replace `keydown` with `keyup`. e.g. this.input.keyboard.on('keyup_A', function (event) { console.log('Hello from the A Key!'); }); I hope this helps and let me know how you get on D jd.joshuadavison 1 Link to comment Share on other sites More sharing options...
jd.joshuadavison Posted May 11, 2018 Author Share Posted May 11, 2018 Thanks Doug! I really wish I'd found the labs earlier! That's exactly what I'm looking for. In fact, Phasers keydown event works differently from the regular javascript onkeydown. Javascript's onkeydown will keep firing repeatedly if you hold the key down, Phasers keydown only fires once, so I think I prefer keydown to keyup! Here's my new event listener in the create function. this.input.keyboard.on('keydown_UP', function (event) { if (player.body.onFloor()) { player.body.setVelocityY(-400); player.anims.play('idle', true); jumpCount = 1; jumpTime = new Date().getTime(); } else if (jumpCount === 1 && ((new Date().getTime() - jumpTime) < 1500)) { player.body.setVelocityY(-300); player.anims.play('idle', true); jumpCount = 0; } }); This certainly feels more right than what I'd written in the update function. Thanks, Josh Link to comment Share on other sites More sharing options...
Doug Posted May 12, 2018 Share Posted May 12, 2018 You're most welcome. I assume you've found the documentation here too: https://photonstorm.github.io/phaser3-docs/list_namespace.html Between the labs and the docs you should have everything you need. Enjoy! It's a great framework. Link to comment Share on other sites More sharing options...
Recommended Posts