删除cocos2d中的精灵

时间:2011-10-26 04:35:52

标签: iphone cocos2d-iphone sprite

-(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];
         }
    }

}

我期待这段代码删除精灵,但它不起作用。精灵仍然在屏幕上。我在这里做错了,请帮忙。感谢。

编辑:

[[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];

1 个答案:

答案 0 :(得分:4)

Ure正在从self删除精灵,但精灵不是self的孩子。这是spriteSheet的孩子,因为你写过:

[spriteSheet addChild:player];

所以你必须从spriteSheet而不是self中删除精灵。但它更容易:

[sprite  removeFromParentAndCleanup: YES];