我需要更改什么才能在移动单元 phone 浏览器上使用以使播放器在屏幕触摸时自动跳转

What do I need to change for use on a mobile cell phone browser to make the player automatically jump when the screen touch

我需要更改什么才能在移动手机 phone 浏览器上使用,使播放器在点击屏幕时自动 运行 向右跳转?我已经搜索了很多年,但找不到答案,我正在努力学习

这是代码

update(time, delta) {
    moveBackgroundPlatform(this.mountainGroup, this.mountainWidth, 'mountains', 0.5);
    moveBackgroundPlatform(this.plateauGroup, this.plateauWidth, 'plateau', 1.5);
    moveBackgroundPlatform(this.groundGroup, this.groundWidth, 'ground', 4);

    if (this.health <= 0) {
        const myUrl = `${fetchScoreData.apiUrl + fetchScoreData.apiKey}/scores`;

        fetchScoreData.postScores(myUrl, { user: gameState.playerName, score: gameState.score });

        this.gameTheme.stop();
        this.scene.stop();
        this.scene.start('GameOver');
    }

    if (this.missileScore >= 1) {
        this.health += 1;
        this.missileScore -= 1;
    }

    this.player.anims.play('run', true);
    this.birdGroup.children.iterate((child) => {
        child.anims.play('fly', true);
    });

    this.missileGroup.children.iterate((child) => {
        child.x -= 5;
    });

    this.timer += delta;
    if (this.timer >= 5000) {
        this.createMissile(415, 'missile');
        this.timer = 0;
    }

    this.secondTimer += delta;
    if (this.secondTimer >= 7000) {
        this.createMissile(300, 'missile2');
        this.secondTimer = 0;
    }

    if (Phaser.Input.Keyboard.JustDown(this.cursors.up)) {

        //this.input.on('pointerdown',  this.jump, this);
        console.log(' up preess');
        if (this.player.body.touching.down || (this.jump < this.jumpTimes && (this.jump > 0))) {
            this.player.setVelocityY(-400);
            this.jumpSound.play();

            if ((this.player.body.touching.down)) {
                this.jump = 0;
            }
            this.jump += 1;
        }
    }

    if (!this.player.body.touching.down) {
        this.player.anims.play('jump', true);
    }

    if (this.cursors.down.isDown) {
        if (!this.player.body.touching.down) {
            this.player.setGravityY(1300);
        }
    }

    if (this.player.body.touching.down) {
        this.player.setGravityY(800);
    }
}
}

export default Game;

有很多方法可以解决这个问题,我不是专业人士,但我会在场景的create方法中添加一个指针事件监听器:

this.input.on('pointerdown', jump, this);

jump方法,它包含所有跳转逻辑:

jump(){
    if (this.player.body.touching.down || (this.jump < this.jumpTimes && (this.jump > 0))) {
        this.player.setVelocityY(-400);
        this.jumpSound.play();

        if ((this.player.body.touching.down)) {
            this.jump = 0;
        }
        this.jump += 1;
    }
}

您可以在 update 函数中调用此 jump 方法以防止代码重复。

update(time, delta) {
    ...
    if (Phaser.Input.Keyboard.JustDown(this.cursors.up)) {
        jump();
        ...
    }
    ...
}