我想通过对齐代表百分之一的多个图像来创建我自己的健康指标。因此,基本上,基于当前的健康状况,我会根据需要对齐所有百分之一的部分。然而,删除它们似乎是一个问题。
-(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];影响?
发布于 2011-12-28 04:36:02
只有一个具有给定标记的视图被删除。
但是,您可以使用以下代码扩展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,但尚未实现。
https://stackoverflow.com/questions/8648434
复制相似问题