Jump to content

Removing sprite from group messes up index returned from forEachAlive


tackle
 Share

Recommended Posts

I have a function 'checkHit' that calls..

this.forEachAlive(this.calculateHit,this.game.physics,hitCirc,this);

..where "this" in this case is a extended Group object.

 

I do a check if enemies are hit when this is called (inside calculateHit), and if I hit an enemy that enemy is removed from the group.

 

The problem then seems to be that since the enemy is removed, the iteration through all the enemies alive fails when it comes to the end - it looks for the last index but there is one index less now in the group.

 

So I get this error:

Uncaught TypeError: Cannot read property 'alive' of undefined 

Is my design fundamentally flawed initially, or should the Group function forEachAlive check for undefined first?

Link to comment
Share on other sites

My solution to this is a to use a temp array as a list of objects to be removed:

var deleteme = [];group.forEachAlive(function(child) {    //Do Stuff...    deleteme.push(child);}for ( var ii = 0; ii< deleteme.length; ii++){    group.remove(deleteme[ii]);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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