HTML5 游戏 Phaser.io 使武器朝 sprite 方向开火

HTML5 game Phaser.io make weapon fire in sprite direction

我正在尝试用 phaser.io 制作一个小游戏,但我遇到了一个小问题。

我使用 Phaser 的武器对象让我的英雄发射子弹。

它工作正常,除了当我向左转时,武器一直向右开火。

这是我的代码(打字稿):

this.weapon = game.add.weapon(1500, 'shot');
this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
this.weapon.bulletSpeed = 750;
this.weapon.fireRate = 2;
this.weapon.bulletAngleVariance = 3;
this.weapon.trackSprite(this, 0, 0, true);

这是移动函数:

walk = (direction) => {

    var speed;
    if (this.isGrounded) {
        speed = this.accel;
    } else {
        speed = this.airAccel;
    }
    if (direction == "right") {
        if (this.body.velocity.x < this.maxSpeed) {
            this.body.velocity.x += speed;
            this.animations.play('walk', 9, true);
            this.scale.x = 1;
        }

    } else if (direction == "left") {
        if (this.body.velocity.x > -this.maxSpeed) {
            this.body.velocity.x -= speed;
            this.animations.play('walk', 9, true);
            this.scale.x = -1;
        }
    }
}

和火灾事件:

if (this.gamepad.justPressed(Phaser.Gamepad.XBOX360_X)) {
    this.weapon.fire(null);
}

我是 Phaser 的新手,所以如果您在我的代码中看到一些奇怪的地方,请告诉我,我想学习 ;-)

谢谢大家的帮助。

根据你的移动方向改变weapon.fireAngle即可。

if (direction == "left")
{
    this.weapon.fireAngle = Phaser.ANGLE_LEFT;
}
else if (direction == "right")
{
    this.weapon.fireAngle = Phaser.ANGLE_RIGHT;
}