我想通过对齐代表百分之一的多个图像来创建我自己的健康指标。所以基本上,根据当前的健康状况,我根据需要调整了百分之几的部分。但是,删除它们似乎是一个问题。
-(void)updateHealthIndicator:(ccTime)delta{
//getting health and healthReduction (removed for better readability). This part does not affect the functioning of the loop...
if(health-healthReduction > 0 ){
NSLog(@"updatehealthindicator called ! health = %d ", health);
health -= healthReduction;
[self removeChildByTag:1000 cleanup:YES];
for (int i = health; i>0; i--){
onePercent = [CCSprite spriteWithFile:@"onepercentofhi.png"];
onePercent.anchorPoint = ccp(0,0);
onePercent.position = ccp(880+(-onePercent.contentSize.width) * i,712 );
[self addChild:onePercent z:2 tag:1000];
}
}
健康指示器显示,但它似乎只删除了第一个“百分之一”的片段。标签1000的所有精灵都受此[self removeChildByTag:1000 cleanup:YES];
影响吗?
答案 0 :(得分:1)
仅删除具有给定标记的一个视图。
但是,您可以使用以下代码扩展CCNode以删除所有子项
-(void) removeChildrenByTag:(int)aTag cleanup:(BOOL)cleanup
{
NSAssert( aTag != kCocosNodeTagInvalid, @"Invalid tag");
int w=[children count]-1;
while(w>=0){
CocosNode *node=[children objectAtIndex:w];
if( node.tag == aTag ){
[self detachChild:node cleanup:cleanup];
}
w--;
}
}
注意:这是要集成到Cocos2D中的proposed solution,但还没有成功。