2011-10-26 33 views
1
-(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]; 
     } 
    } 

}在cocos2d

卸下精靈我期待這個代碼刪除精靈,但它不工作。精靈仍然在屏幕上。我在這裏做錯了,請幫忙。謝謝。

編輯:

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

作爲Sprite「obj」是動畫精靈。我正在使用spritesheet,可能是這裏的問題嗎? – xibic

+0

請顯示代碼如何創建精靈 – Andrew

+0

那裏我正在創建精靈。 – xibic

回答

4

茜從self移除精靈,但精靈是不是self一個孩子。它的spriteSheet一個孩子,因爲你寫的:

[spriteSheet addChild:player]; 

所以,你必須從spriteSheet,而不是刪除自己的精靈。但它更容易:

[sprite removeFromParentAndCleanup: YES]; 
+0

謝謝! !有效。 – xibic