Jump to content

How to kill bullets using Phaser's weapon plugin


microhacker
 Share

Recommended Posts

I am using Phaser 2.5.0 and using the weapon plugin that comes with it. I been trying for a week to get my bullet for my game to be killed or destroyed when it hits a meteor. So far I have been only been able to kill the meteor.
Below is the code from my game state that includes my weapon and the meteors

MeteorCrisis.StateGame = function(game) {
	//Variables
	var meteors;
	var weapon;
};

MeteorCrisis.StateGame.prototype = {

create: function() {
        this.physics.startSystem(Phaser.Physics.ARCADE);
  	//Player Laser Gun
	weapon = this.add.weapon(-1, 'laser');
	weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
	weapon.bulletSpeed = 600;
	weapon.fireRate = 400;
        //Meteors
	meteors = this.add.group();
	meteors.enableBody = true;
	meteors.physicsBodyType = Phaser.Physics.ARCADE;
	this.time.events.loop(1400, this.createMeteor, this);
},
createMeteor: function() {
        var meteor = meteors.create(800, this.world.randomY - 40, 'meteor');
},
update: function() {
	//When a bullet collides with a meteor 
	this.physics.arcade.overlap(weapon.bullets, meteors, this.laserHitMeteor, null, this);
},
//This is the function that is called when the bullet hits the meteor
laserHitMeteor: function(meteor, meteor) {

	meteors.remove(meteor, true);
}
};

I used meteor twice because weapon.bullets would break my game state. Any help on how I wrote my functions wrong or how to kill or destroy the bullet would be greatly appreciated!

Link to comment
Share on other sites

The error is in your laserHitMeter function. You cannot have 2 arguments with the same name, as the 2nd one is replacing the first. The first should be called 'bullet' or something similar.

Also are you sure you want to actually remove the meteor? Or just kill it? (meteor.kill())

Link to comment
Share on other sites

Thank both you guys! This really helps!

4 hours ago, rich said:

The error is in your laserHitMeter function. You cannot have 2 arguments with the same name, as the 2nd one is replacing the first. The first should be called 'bullet' or something similar.

Also are you sure you want to actually remove the meteor? Or just kill it? (meteor.kill())

I knew the function was wrong, it was a temporary fix for me. Also I am sure to remove the meteor because I have a loop that spawns in new meteors.

3 hours ago, yahnpau said:

Hi microhacker. :)

Have you tried using killAll(); ?

docs/src_plugins_weapon_WeaponPlugin.js.html, line 540

This was useful and fixed my problem of my bullet not dying when it hits a meteor!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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