HTML5游戏Phaser.io以精灵方向开火

时间:2016-10-02 20:37:51

标签: javascript html5 phaser-framework

我试图用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的菜鸟,所以如果你在我的代码中看到一些奇怪的东西,请告诉我,我想学习; - )

谢谢你们的帮助。

1 个答案:

答案 0 :(得分:1)

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

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