Jump to content

Killing wrong sprite in group?


AGoodUsername
 Share

Recommended Posts

I have an enemy spawner which spawns enemies (duh) onto the playfield. I clone them using a group like so:

   	this.enemy = this.enemies.getFirstDead();
   	this.enemy.reset(x, y);
   	this.enemy.body.velocity.x = 200;

 

When it collides with something, it kills the enemy. This works perfectly fine if there is one enemy, but once there are two enemies, I run into problems. When the first enemy collides with the hero, the SECOND enemy dies instead of the first. The second one spawned AFTER the first. How would I kill the first and not the second? KIll function:

kill: function () {
   	this.enemy.kill();
},

 

Link to comment
Share on other sites

Hello,

you don't provide much context so it's hard to tell. But this.enemy.kill(), why do you do this? Why don't you just set the function for collision callback? How does your collision call look like?

Check the docs for details, collision function gives you only the object which collide together which is precisely what yo uare lookign for.

Your collision callback could look something like this:
 

function (hero, enemy) {

  enemy.kill();

}

 

Link to comment
Share on other sites

On 16-03-2016 at 7:37 AM, AzraelTycka said:

Hello,

you don't provide much context so it's hard to tell. But this.enemy.kill(), why do you do this? Why don't you just set the function for collision callback? How does your collision call look like?

Check the docs for details, collision function gives you only the object which collide together which is precisely what yo uare lookign for.

Your collision callback could look something like this:
 


function (hero, enemy) {

  enemy.kill();

}

 

Hi, thanks for your reply. I don't want to share my whole code publicly, so is there a way I can send it to just you?

Link to comment
Share on other sites

I don't think it is needed as long as what you are looking is covered by the function above.

I use in one of my projects arcade physics and I call for collision in update like this:

this.physics.arcade.overlap(this.items.bubbles, this.items.aquarium, this.killBubble, 0, this);

Well instead of overlap you can use '...arcade.collide', for my particular case overlap was giving the wanted result. Either you use overlap or collide is on you their call is the same. In my case I call overlap on groups called bubbles and aquarium stored inside this.items object, next the code above says that if they collide this.killBubble method should be called (next argument is 0 and this, check docs for more info) and this function will be called with two objects that overlap (not just two groups but those particular objects).

Now the method killBuble could look like this:

killBubble: function(bubble, aquarium)
{
   bubble.kill();
   aquarium.spawnNewBubble();
}

As you can see overlap/collide gives you only the objects which overlap/collide which is what you are looking for. Only replace bubble and aquarium with your stand ins and you are done.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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