xibic
xibic

Reputation: 27

Removing sprites in cocos2d

-(void) update:(ccTime *)dt{
    for( int i=0; i < [objects count]; i++){
         CCSprite *obj = (CCSprite *) [objects objectAtIndex:i];
         if(obj.position.y <= 40){
            [obj stopAllActions];
            [self removeChild:obj cleanup:YES]; /* ?? */
            [objects removeObjectAtIndex:i];
         }
    }

}

I am expecting this code to remove the sprites but its not working. sprite is still on the screen. what i am doing wrong here , please help. thanks.

EDIT:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vase.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"vase.png"];
[self addChild:spriteSheet z:1 tag:1];
//CCSpriteFrame must be created for each animation frame.
NSMutableArray *animFrames = [NSMutableArray array];
for (int i = 1; i <= 3; i++) {
    NSString *file = [NSString stringWithFormat:@"frame %d.png", i];
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:file];
    [animFrames addObject:frame];
}
//Sprite is positioned and then animated.
CCSprite *player = [CCSprite spriteWithSpriteFrameName:@"frame 1.png"];     
CCAnimation *anim = [CCAnimation animationWithFrames:animFrames delay:0.20f];
CCRepeat *repeat = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:YES] times:3];


[player runAction:repeat];

player.position = startPostion;
[spriteSheet addChild:player];

Upvotes: 1

Views: 1827

Answers (1)

Andrew
Andrew

Reputation: 24866

Ure are removing the sprite from self, but the sprite is not a child of self. It's a child of spriteSheet because you've written:

[spriteSheet addChild:player];

So you have to remove the sprite from spriteSheet instead of self. But it's easier to:

[sprite  removeFromParentAndCleanup: YES];

Upvotes: 4

Related Questions