twoshorts Posted February 7, 2014 Share Posted February 7, 2014 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:group1Object1 spawns group1Object2 spawns group1Object1 hits obj1 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 More sharing options...
rich Posted February 7, 2014 Share Posted February 7, 2014 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. Link to comment Share on other sites More sharing options...
XekeDeath Posted February 7, 2014 Share Posted February 7, 2014 The colliding objects are passed to the collision handler in the order you put them in the collision test.game.physics.collide(sprite, group, ...)collision(sprite, groupSprite) Link to comment Share on other sites More sharing options...
twoshorts Posted February 7, 2014 Author Share Posted February 7, 2014 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 More sharing options...
Nokdu Posted February 7, 2014 Share Posted February 7, 2014 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 More sharing options...
XekeDeath Posted February 7, 2014 Share Posted February 7, 2014 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 More sharing options...
twoshorts Posted February 9, 2014 Author Share Posted February 9, 2014 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 More sharing options...
jcs Posted February 9, 2014 Share Posted February 9, 2014 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... twoshorts 1 Link to comment Share on other sites More sharing options...
twoshorts Posted February 9, 2014 Author Share Posted February 9, 2014 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 More sharing options...
Recommended Posts