0
\$\begingroup\$

I have a platforming game, written in javascript with the Phaser.js framework, where my character collects objects. I thought trying to collect all objects in the least amount of jumps would be interesting, so I tried to implement a jump counter. Relevant pieces of code:

var jumpCount = 0;
var jumpText;

function create (){
    jumpText = this.add.text(16, 48, 'Jumps Used: 0', {fontsize: '32px', fill: '#000'});
}

function update (jumpCount){
    if (cursors.space.isDown && player.body.touching.down){
        player.setVelocityY(-630);
        jumpCount++;
        jumpText.setText('Jumps Used: ' + jumpCount);
    }
}

This is displaying the counter and incrementing it sort of correctly, but it seems to count the amount of time that the spacebar is held down and is leading to jump counts that are floats well into the thousands.

Is there a more reasonable way to handle counting the number of times a player has manually jumped?

Thanks!

\$\endgroup\$
0
\$\begingroup\$

Instead of checking in the update you can instead add a binding to the key itself. In Phaser 2, which it looks like you might be using:

jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
jumpButton.onDown.add(jump, this);

function jump() {
    // Add your existing logic here.
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.