代码之家  ›  专栏  ›  技术社区  ›  Hadrien Delphin

HTML5游戏Phaser。io使武器朝精灵方向开火

  •  0
  • Hadrien Delphin  · 技术社区  · 8 年前

    我正试着用移相器做一个小游戏。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 回复  |  直到 8 年前
        1
  •  1
  •   Li Dawei    8 年前

    只要换武器。fireAngle根据您的移动方向。

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