I can’t use shift() or splice() and it’s driving me insane:
var glength:int = gatActivePool.length;
for (var a:int = 0; a < glength; a+=1) {
var gatling:PBulletGat = gatActivePool[a];
if (gatling.dead) {
// If it's not already in the InactivePool, push it in
if (gatInactivePool.indexOf(gatling) < 0) gatInactivePool.push(gatling);
// Remove it from the ActivePool...
// CAUSES NULL OBJECT ERROR:
//gatActivePool.shift();
// SAME:
gatActivePool.splice(gatActivePool.indexOf(gatling), 1); // doesn't work with splice(a, 1) either
} else {
gatling.update();
}
}
As you probably guessed, this is for a game. Elsewhere, in a shoot() method I have this:
var gat:Gat1 = gatInactivePool.shift();
gatActivePool.push(gat);
gat.spawn();
So while the player is shooting (adding to the active pool), the game will add the bullet back to the inactive pool if it leaves the screen. So there’s a lot of shifting going on, which is why I figured I was getting a null return when I used splice() (hard to get an index position if the index keeps changing), so I tried shift instead to blindly remove the first element, but that didn’t work either.
So does anyone have any idea why I can’t remove anything from the gatActivePool array at all? Is there a better way to recycle particles?