Jump to content

Remove object from group on collision 


override
 Share

Recommended Posts

How can i remove a object from a group on a collision?

This is what i have so far

// preload
preload: function() {    
    this.coin = game.add.group(); // Create a group  
    this.coin.enableBody = true;  // Add physics to the group  
    this.coin.createMultiple(10, 'coin'); // Create 20 snow   

//in update
update: function() { 
game.physics.arcade.overlap(this.bird, this.coin, this.getCoin(this.coin), null, this);


//in getCoin function
getCoin: function(item){
this.coin.remove(item);

 

Link to comment
Share on other sites

getCoin: function(item){
  this.coin.forEach(function(item) {
        if(game.physics.arcade.overlap(this.bird, item, null, null, this)){
            item.kill();   
            // item.revive();     
          
        }
    }, this);
  
}

Figured it out this is what i have for anyone else who might need it

Link to comment
Share on other sites

You don't need to go through your coin group (for clarity I would call it coins considering it's a group full of coins and coin only one element of that group ;-)) like that.

Overlap works on groups and straight away calls your callback function with items which overlapped.

game.physics.arcade.overlap(this.bird, this.coin, this.killCoin, null, this);

killCoin: function(bird, coin){
            coint.kill();   
            // coin.revive();
}

You are basically forcing phaser to call overlap several times while it already gave you what you wanted.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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