如何使我的角色在Phaser 3中跳跃?

时间:2020-05-09 14:53:30

标签: javascript phaser-framework 2d-games

我是Phaser 3的新手,我想让角色跳起来。

这是我的代码:

create() {
    this.kid = this.physics.add.sprite(50, 380, 'idle');
    this.kid.setScale(.3);
    this.kid.setGravityY(300);
    this.kid.setBounce(0.2);
    this.kid.setCollideWorldBounds(true);
    this.physics.add.collider(this.kid, this.platforms);
    this.cursorKeys = this.input.keyboard.createCursorKeys()
}

update() {
    this.moveKid()
}

moveKid() {
    if (this.cursorKeys.left.isDown) {
        this.kid.setVelocityX(-300)
    } else if (this.cursorKeys.right.isDown) {
        this.kid.setVelocityX(300)
    } else {
        this.kid.setVelocityX(0);
        this.kid.setVelocityY(0);
    }
    if (this.cursorKeys.up.isDown && this.kid.body.touching.down) {
        this.kid.setVelocityY(-300);
    }
}

但是目前,角色只跳了几个像素,仅此而已。如果我卸下touching.down部分,则玩家会自由跳跃,但同时也会跳到空中,并且跌倒得非常非常慢(无论我将其设置为何种重力)。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您希望删除代码中的行,在没有键盘输入的情况下将字符的y速度设置为零。每次游戏更新时,它都会停止角色的动作,因此下降速度很慢。