Jump to content

Check which index of a group is colliding?


twoshorts
 Share

Recommended Posts

I am trying to collide two things, a sprite and a group.

game.physics.collide(obj1, group1, collisionHandler);

I am then trying to kill only the one group member that hits obj1.

 

Order of events:

  1. group1Object1 spawns
  2. group1Object2 spawns
  3. group1Object1 hits obj1
  4. group1Object2 hits obj1

 

This is the code I tried using:

group1.getFirstAlive().kill();

This code seems to randomly choose between killing group1Object1, or Killing group1Object2 and still leaving group1Object1 on the screen.

 

Any ways to fix the code?

 

Synopsis: how do I differentiate between the objects in a group I am colliding with

Link to comment
Share on other sites

When you collide a Sprite vs. a Group, the first parameter sent to your collisionHandler is always the Sprite, the 2nd is the Group member. So you can safely kill it from within that callback.

 

I'm not trying to kill the sprite though. I'm only trying to kill an individual member of the group that hit the sprite. Maybe I'm not understanding something?

Link to comment
Share on other sites

this is how i did it.

use collide() to collide a group with sprite..

game.physics.collide(this.bulletgroup, this.PlayerSprite, RPGTownGame.prototype.collisionHandler, null, this);

and then in the collision handler.. 

collisionHandler(player, bullets:Phaser.Sprite) {    if (!bullets.alive) return;    bullets.kill();}
Link to comment
Share on other sites

A group is just a collection of sprites. Colliding doesn't happen against the group object, it happens against each sprite in the group one at a time.

It is the same as iterating over the group members and calling collide on each one separately.

In the end, the collision is sprite vs sprite, and when they do collide, they are both passed to the collision handler.

 

So, in your case, your collisionHandler function can take 2 arguments, and they will be obj1, and a sprite that is a member of group1.

Link to comment
Share on other sites

Ok this is still not working out for me. Maybe someone can look through my code?

 

function update() {     //Other code     "use strict"     game.physics.collide(coins, player, collectCoin, null, this);}
function shootCoin() { //Creates the coins from the coins group    "use strict";    if (game.time.now > coinTimer) {        coin = coins.getFirstExists(false);        if (coin) {            coin.reset(spot.x, spot.y);            coin.body.velocity.y = 300;        }    }}function collectCoin() { //collisionHandler     "use strict";     coin.kill(); }

 

So what's happening is that whenever a coin is collected, it destroys the newest created coin, rather than the coin that is actually colliding with the player. What do I need to do to fix it?

Link to comment
Share on other sites

you say you're not trying to kill the sprite, but the coin IS a sprite - one of the two involved in the collision.

 

'collectCoin()' is your collision handler. it should take two arguments, which are the colliding sprites, instead of none. you can then take action (such as 'kill') on either (or both) of the sprites that were involved in the collision...

Link to comment
Share on other sites

you say you're not trying to kill the sprite, but the coin IS a sprite - one of the two involved in the collision.

 

'collectCoin()' is your collision handler. it should take two arguments, which are the colliding sprites, instead of none. you can then take action (such as 'kill') on either (or both) of the sprites that were involved in the collision...

 

Ok that was the problem. I didn't realize that I had to put the two sprites into the parameters. Thank you so much!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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