Jump to content

How to access bullets in collide function?


PixelProgrammer
 Share

Recommended Posts

So I'm currently following @rich 's Shoot-em-up tutorial from over here https://phaser.io/tutorials/coding-tips-007. Please have a look at the source code if you haven't already.

From what I understand he create's a weapon array that holds all the weapon types (each of which are an object inside a Weapon object).

Now my question is How do I access the current weapon bullets in game.physics.arcade.collide?

I've used 

game.physics.arcade.collide(this.weapon[this.currentWeapon], this.enemies)

and this works fine, the bullets and the enemy collide well.

BUT what if I want to kill that bullet? How would I pass the specific bullet that collided with the enemy into a function?

This is what I've tried: 

game.physics.arcade.collide(this.weapon[this.currentWeapon], this.enemies, function(bullet, enemy)
  {
    bullet.kill();
  }, null, this);

However the above code doesn't work. Bullet is undefined. What do I do?

Link to comment
Share on other sites

can you do a `console.log(bullet, enemy)` instead of bullet.kill ?

It is entirely possible that:
1) sometimes bullet is undefined, other times it is not so we need to figure out what's going on

2) you may need to use `arcade.overlap` instead of `arcade.collide`

Link to comment
Share on other sites

weapon = function(posx,posy,speed,frequency,angular){
	this.posx=posx
	this.posy=posy
	this.flag_explode=false
	this.speed=speed
	this.angular=angular
	this.frequency=frequency

	//canon
	Phaser.Sprite.call(this,game,this.posx,this.posy,'canon')
	this.anchor.setTo(.5,.5)
	this.angle=this.angular
	game.physics.arcade.enable(this);
	this.weapon=game.add.weapon(9,'bullet')	
	this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
	//  Because our bullet is drawn facing up, we need to offset its rotation:
	this.weapon.bulletAngleOffset = 0;

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

	//  Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
	this.weapon.fireRate = this.frequency ;

	//  Add a variance to the bullet angle by +- this value
	this.weapon.bulletAngleVariance = 0;

	//  Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically
	this.weapon.trackSprite(this,0,0,true);
}


weapon.prototype = Object.create(Phaser.Sprite.prototype)
weapon.prototype.constructor = weapon

//THESE FUNCTION SOLVE YOUR QUESTION

weapon.prototype.touch_bullet=function(item){
		this.weapon.bullets.forEach(function(item){
			if(item.alive){	
				item.visible=false
	}
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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