I'm having trouble firing a bullet from my object in my game. I have been struggling to find the proper reason for the issue. Here is a snippet of my code.
Game.js
var Game = {
preload : function() {
// spaceship image screen
this.load.image('spaceship', './assets/images/spaceship.png');
//Load the bullet
this.load.image('bullet', 'assets/images/bullet.png');
},
This is the create function
create : function() {
// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet', 0, false);
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
bullets.setAll('outOfBoundsKill', true);
bullets.setAll('checkWorldBounds', true);
sprite = game.add.sprite(400, 300, 'spaceship');
sprite.anchor.set(0.5);
},
This is the update function
update: function() {
if (game.input.activePointer.isDown)
{
this.fire();
}
},
The fire function
fire: function () {
if (game.time.now > nextFire && bullets.countDead() > 0)
{
nextFire = game.time.now + fireRate;
var bullet = bullets.getFirstDead();
bullet.reset(sprite.x - 80, sprite.y - 80);
game.physics.arcade.moveToPointer(bullet, 300);
}
}
Please review my code and suggest where I might be going wrong.