Jump to content

destroy() on group children error


metalslug87
 Share

Recommended Posts

hello,

I have an error when using destroy() on group.children[0]

function create() {
    boomerangBullets = this.game.add.group();
    boomerangBullets.enableBody = true;  
    boomerangBullets.physicsBodyType = Phaser.Physics.ARCADE;
    boomerangBullets.setAll('checkWorldBounds', false);
    boomerangBullets.setAll('outOfBoundsKill', true);
}

function update() {
     if(game.physics.arcade.distanceBetween(player, boomerangBullets.children[0]) < 30) {
        boomerangBullets.children[0].destroy();
     }
}

here is an error screenshot --> http://prntscr.com/cnea7s
Please help me, the kill() method is working fine but I need to completely destroy the group children element.

Link to comment
Share on other sites

Just a guess as I can't see all code:

I guess you have an object called "a" as boomerangBullets.children[0]. The function Math.atan2 is looking for the x/y value of that object (a.x and a.y). When you kill an object, it simply disappears but can still be called by the game (hence why that works fine). When you destroy an object, it gets completely removed. So it is likely that one part of your code is still calling Math.atan2 on object "a" after it got destroyed, resulting in this error.

Link to comment
Share on other sites

thanks! I've made a correction (undefined typeof condition) and it's working fine :)

if (boomerangReturn == true && typeof boomerangBullets.children[0] != "undefined") {
        game.physics.arcade.moveToXY(boomerangBullets.children[0], player.body.position.x, player.body.position.y, 200, 0);
        if(game.physics.arcade.distanceBetween(player, boomerangBullets.children[0]) < 30) {
            boomerangBullets.children[0].destroy();
        }
};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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