Jump to content

Phaser change fire function direction


CharlesCraft50
 Share

Recommended Posts

I am making my own top view game and i just added a weapon, it is a pistol,
I want to change the direction of the bullets by not using angleVelocity because if I use it, the pistol will rotate and i dont like it. i just want to change the direction of the bullets like if you click the left button, the pistol will change its direction to the left and also the bullet.


Sample Code:
 

      function create() {
      //some update function code...
      player = game.add.sprite(100, 390, 'player');
      weapon = game.add.weapon(ammo, 'bullet');
      weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
      weapon.bulletAngleOffset = 90;
      weapon.bulletSpeed = 600;
      weapon.fireRate = 500;
      //The pistol that follow the player
      pistol = game.add.sprite(player.body.x, player.body.y, 'Pistol');
      game.physics.arcade.enable(pistol);
      pistol.scale.setTo(0.5, 0.5);
      weaponType = "";
      weapon.trackSprite(pistol, 0, 0, true);
      fireButton = game.input.keyboard.addKey(Phaser.Keyboard.Q);
      }

      function update() {
      //some update function code...
      if (fireButton.isDown) {
          if(facing == "left") {
             weapon.fire(); //I want this to fire in the left direction
          } else if(facing == "right") {
             weapon.fire(); //I want this to fire in the right direction
          } else if(facing == "up") {
             weapon.fire(); //I want this to fire in the up direction
          } else if(facing == "down") {
            weapon.fire(); //I want this to fire in the down direction
          }
      }
      }

 

Link to comment
Share on other sites

if (fireButton.isDown) {
    if (facing == "left") {
        weapon.fireAngle = Phaser.ANGLE_LEFT;
    } else if (facing == "right") {
        weapon.fireAngle = Phaser.ANGLE_RIGHT;
    } else if (facing == "up") {
        weapon.fireAngle = Phaser.ANGLE_UP;
    } else if (facing == "down") {
        weapon.fireAngle = Phaser.ANGLE_DOWN;
    }
    weapon.fire();
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...