Phaser Can't get a character to climb a ladder right

Viewed 40

I'm making something of a platform game, and need to get a character to rise when they press a button while in collision with a ladder like sprite. Here is the code I have to do this:

if (this.checkCollision(this.p1, this.ropeSpot) && Phaser.Input.Keyboard.JustDown(this.keyT)){
        inventory.splice(inventory.indexOf("rope"));
        this.ropeSpot.destroy();
        this.rope = this.physics.add.sprite(628, 420, 'ropeClimb');
        this.rope.body.immovable = true;
        this.rope.body.allowGravity = false;
        if (this.checkCollision(this.p1, this.rope) && this.keyT.isDown){
            this.p1.body.setVelocityY(-200);
        }
    }

The player has to be in collision with the ropeSpot sprite, which is a clear sprite showing marking where a button can be pressed for an interaction, and then press the T button to destroy the ropeSpot and then spawn the rope sprite, a tall, thin sprite.

If the player presses T, which I plan to use as a general interaction button, while in collision with the rope sprite, they should rise until they release the button or are no longer colliding with the rope sprite. This doesn't occur as intended, instead resulting as a small jump.

I then tried to alter the code into this:

if (this.checkCollision(this.p1, this.ropeSpot) && Phaser.Input.Keyboard.JustDown(this.keyT)){
        inventory.splice(inventory.indexOf("rope"));
        this.ropeSpot.destroy();
        this.rope = this.physics.add.sprite(628, 420, 'ropeClimb');
        this.rope.body.immovable = true;
        this.rope.body.allowGravity = false;
        if (this.checkCollision(this.p1, this.rope) && this.keyT.isDown){
            this.p1.body.allowGravity = false;
            this.p1.body.setVelocityY(-200);
        }
        else if (!this.checkCollision(this.p1, this.rope) || !(this.keyT.isDown)){
            this.p1.body.allowGravity = true;
        }
    }

While this did cause the character to rise when the button is pressed, it couldn't stop when the button was let go or the character moved away from the rope sprite. The character only stopped when it hit something immovable, like a platform or the top of the screen. And when the character did stop, it wouldn't fall back down again, leaving it floating but still able to move left and right.

I'm unsure of what to try next, or even what I did wrong. Can someone offer a solution?

If it helps, I'm using Phaser 3 in VSCode, employing arcade physics.

1 Answers

I would simply, move the inner if- clause out of the outer if- clause, and add a check if this.rope exists, this should solve the issue.

Since in the posted code the first if clause will be entered only once, because this.ropeSpot is destroyed, so it can never set the gravity on again.

if (this.checkCollision(this.p1, this.ropeSpot) && Phaser.Input.Keyboard.JustDown(this.keyT)){
    ...
}
//only execute if this.rope is set
if (this.rope && this.checkCollision(this.p1, this.rope) && this.keyT.isDown){
   ...
}
else if (this.rope && !this.checkCollision(this.p1, this.rope) || !(this.keyT.isDown)){
    ...
}
Related