1
votes

Have been working on a Tower Defense game for iOS for some time now. I am using Cocos2d (v0.99.5 not to sure). Recently started to get into some problems when I started remove sprites.

Have profiles using instruments and Zombies and here's what I get:

Zombie trace The code for updating the sprites (and also delete them) is located in update:

-(void) update:(ccTime)deltaTime {
if (paused)
    return;

CCArray *childs = [textureAtlas children];

//Update all objects
for (GameSprite *tempChar in childs) {
    if ([tempChar respondsToSelector:@selector(updateStateWithDeltaTime:andListOfGameObjects:andAtlas:)]) {
        [(GameSprite*)tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:[textureAtlas children] andAtlas:textureAtlas]; 
    }
}

//Remove dead projectiles
for (GameSprite *tempChar in childs) {          
    if ([tempChar isKindOfClass:Projectile.class]) {  //(**)
        if (![tempChar alive]) {
            [tempChar remove];
        }
    }
}

//Remove dead towers
for (GameSprite *tempChar in childs) {
    if ([tempChar isKindOfClass:Tower.class]) {
        if (![tempChar alive]) {
            [tempChar remove];
        }
    }
}
//Remove dead creeps
for (GameSprite *tempChar in childs) {
    if ([tempChar isKindOfClass:Creep.class]) {
        if (![tempChar alive]) {
            [tempChar remove];
        }
    }
}
}

The remove method in GameSprite.m :

-(void)remove {
    CCLOG(@"remove");
    [self removeFromParentAndCleanup:YES];
}

The first block updates the sprites in the SpriteBatchNode/textureAtlas. The three remaining blocks checks the different objects if the should be deleted. (their alive-variable set to NO?). The reason I have three blocks of different types is due to the fact that projectiles have (weak) reference to creep they are shooting at and needs to be deleted before the creep.

So my problem is that it randomly crashes when a projectile hits a creep and the projetile (and all other projectiles shooting at that creep) are to be removed). The creeps reference count gets down to 0 but seems to still be in the childs array. Cause the error I get is:

*** -[NormalProjectile isKindOfClass:]: message sent to deallocated instance 0xff33e30

on the row I marked with (**)

Either the problem is with me understanding Cocos2d's removeFromParentAndCleanUP: or that my "solution" for handling the projectile-creep-relationship is bad from a memory point of view. Any ideas on how to solve this or debug it further?

1

1 Answers

3
votes

Your fast enumeration techniques for going through the array are casting all your sprites in that array as a custom subclass of CCSprite, then checking to see if they are instead a different custom subclass, like Tower, etc. I don't see this as healthy programming practice, since methods in one class may not be in the other, yet you are casting them nonetheless.

That may or may not be contributing to your issue, but a wiser approach is to keep an assign iVar CCArray for children of a specific class. i.e. put all your projectiles into one array at the time they are created and added to the batch node then you can iterate through all projectiles, towers, etc by going through those individual arrays, where you know what kind of class they already are. Then remove them from the array as well as from the parent when necessary. I would think this is quite a bit safer than going through all the game's sprites in their entirety that are in your batchnode, which may include spites not being used, perhaps, and casting them and testing them as different classes.