Jump to content

how do double jump?


jopcode
 Share

Recommended Posts

Hello, I want to make a double jump but with my code it's really hard the double jump, you need to press the key too fast to make it.

any idea ?

this.onTheGround = this.player.body.blocked.down;
    if(this.onTheGround) {
      this.jumps = 3;
      this.jumping = false;
    }
    // Jump!
        if (this.jumps > 0 && this.cursors.up.isDown) {
            this.player.body.velocity.y = -350;
            this.jumping = true;
        }

        // Reduce the number of available jumps if the jump input is released
        if (this.jumping && this.cursors.up.isDown) {
            this.jumps--;
            this.jumping = false;
            console.log('jumps avalible:' + this.jumps);
        }

 

Link to comment
Share on other sites

Hi, first part, resetting your jump properties if on ground, is OK:

    this.onTheGround = this.player.body.blocked.down;
    if(this.onTheGround) {
      this.jumps = 3;
      this.jumping = false;
    }

Second part is problematic. To check if key was just pressed use justDown instead of isDown. Also code logic seems little bit strange to me.

 

In attachment you will find complete small endless runner example. I implemented your triple jump into it. It is written in TypeScript, but you should have no problems to rewrite it into JS, if you need.

SimpleRunner.zip

Crucial parts are:

 + define these variables for jumping:

    // jumping
    private _nextJumpTime: number = 0;
    private _jumping: boolean = false;
    private _jumps: number = 3;

 + in update see code for jumping (key used for jumping is space in example):

        let onGround = this.physics.arcade.collide(this._player, this._platforms);

        let body = <Phaser.Physics.Arcade.Body>this._player.body;

        let canJump = onGround && this.game.time.time > this._nextJumpTime;
        let canJumpInAir = this._jumping && this._jumps > 0;

        if (canJump) {
            this._jumping = false;
            this._jumps = 3;
        }

        if (this._input.justDown && (canJump || canJumpInAir)) {
            if (onGround) {
                this._jumping = true;
                this._nextJumpTime = this.game.time.time + 250;
            }

            --this._jumps;

            body.velocity.y = -850;
        }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...