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

问题描述 投票:0回答:1

我是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”部分,则可以自由跳跃,但同时也会跳跃,并且坠落非常缓慢。不管重力如何设置。有什么帮助吗?非常感谢!

javascript phaser-framework 2d-games
1个回答
0
投票

您希望删除代码中的行,在没有键盘输入的情况下,将字符的y速度设置为零。它抵消了每次更新中的重力影响,因此降低了缓慢的下降速度。

© www.soinside.com 2019 - 2024. All rights reserved.