【问题标题】:HTML5 game Phaser.io make weapon fire in sprite directionHTML5 游戏 Phaser.io 使武器在精灵方向上开火
【发布时间】:2016-12-23 10:26:28
【问题描述】:

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

谢谢你们的帮助。

【问题讨论】:

  • 我在这里没有看到一行打字稿。

标签: javascript html phaser-framework


【解决方案1】:

只需根据您的移动方向更改武器.fireAngle。

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

【讨论】:

    猜你喜欢
    • 2016-06-13
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多