Holding button to jump higher with Phaser JS - Skate Platformer Game Devlog #04

Written in September 21, 2020 - Games

Hey, I’m back with another Game Devlog.

// Only allow the hero to jump if they are on the ground
let willJump = false;
if (
    onGround
    && (
        Input.Keyboard.JustDown(this.controlKeys.up)
        || Input.Keyboard.JustDown(this.controlKeys.w)
    )) {
    // hero is on the ground, so he is allowed to start a jump
    this.jumptimer = 1;
    this.body.setVelocityY(-200);
    this.setAnimation('jump');
    willJump = true;
} else if (
    this.jumptimer !== 0
    && (
        this.controlKeys.up.isDown
        || this.controlKeys.w.isDown
    )) {
    // hero is no longer on the ground, but is still holding the jump key
    this.jumptimer += 1;
    if (this.jumptimer > 8) {
        // hero has been holding jump for over 100 millliseconds, it's time to stop him
        this.jumptimer = 0;
    } else if (this.jumptimer > 7) {
        // hero is allowed to jump higher, not yet 600 milliseconds of jumping
        this.body.setVelocityY(-200);
    }
} else if (this.jumptimer !== 0) {
    // reset this.jumptimer since the hero is no longer holding the jump key
    this.jumptimer = 0;
}

This code is originally from this forum post.

Tags:


Post a comment

Comments

No comments yet.