Jump to content

Weapon plugin- How to disable autofire


igkman
 Share

Recommended Posts

var player;
var keys;
var shootButton;
var bullet;

function create() {

	//game.add.sprite(0,0, 'background');
	game.physics.startSystem(Phaser.Physics.ARCADE);

	//creates player
	player = game.add.sprite(0.45*600,600-50,'galaga');
	game.physics.arcade.enable(player);

	// allows player to fire 2 bullets 
	bullet = game.add.weapon(2,'bullet');

	// when bullet leaves the screen, it will be destroyed
	bullet.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;

	// offset rotation
    bullet.bulletAngleOffset = 90;

    // prevents ship from auto firing
	bullet.autofire = false;

    // The speed at which the bullet is fired
    bullet.bulletSpeed = 400;

    //keeps track of how many bullets were shot
    bullet.shots = 0;

    //  Tell the bullet to track the 'player' Sprite, offset by 16px horizontally, 0 vertically
    bullet.trackSprite(player, 16, 0);

	//enabling keyboard use
	keys = game.input.keyboard.createCursorKeys();
	shootButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}

function update() {

	// Reset the players velocity (movement)
	player.body.velocity.x = 0;

	if (keys.left.isDown) {
		// Move to the left
		player.body.velocity.x = -150;
	}
	else if (keys.right.isDown) {
		// Move to the right
		player.body.velocity.x = 150;
	}
	else  {
		//ship is idle
	}
	//shooting the bullet
	if (shootButton.isDown) {
		bullet.fire();
	}
}

What I'm trying to do is prevent my player variable from autofiring(holding down space button to fire bullets.). I want the player to be able to shoot two bullets at a time, but only when the player presses the fire button rather than holding it. I was under the impression that bullet.autofire = false; would do the trick, but I was wrong. Am I using the autofire variable the wrong way?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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