igkman Posted October 28, 2016 Share Posted October 28, 2016 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 More sharing options...
squilibob Posted October 29, 2016 Share Posted October 29, 2016 I don't see you setting onFireLimit to 2 anywhere in your code? Link to comment Share on other sites More sharing options...
igkman Posted October 30, 2016 Author Share Posted October 30, 2016 Isn't this snippet of code supposed to do that? bullet = game.add.weapon(2,'bullet'); Link to comment Share on other sites More sharing options...
samme Posted October 31, 2016 Share Posted October 31, 2016 Autofire simply means the weapon fires continuously at its maximum rate. It's off by default. You can attach a listener to shootButton.onUp (in create) instead of testing `shootButton.isDown` during update. In game.add.weapon(), quantity = 2 sets the initial size of the bullet pool. Link to comment Share on other sites More sharing options...
Recommended Posts