Jump to content

Weapon plugin- How to disable autofire


Recommended Posts

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

function create() {

	//game.add.sprite(0,0, 'background');

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

	// 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) {

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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