我在ccarray中添加了三种精灵。
CCArray *storeObject=[[CCArray alloc]initWithCapacity:10];
CCSprite *sprite1=[CCSprite spriteWithFile:@"sprite1.png"];
CCSprite *sprite2=[CCSprite spriteWithFile:@"sprite2.png"];
CCSprite *sprite3=[CCSprite spriteWithFile:@"sprite3.png"];
[storeObject addObject:sprite1];
[storeObject addObject:sprite2];
[storeObject addObject:sprite3];
[self addChild:sprite1 z:1 tag:100];
[self addChild:sprite2 z:1 tag:101];
[self addChild:sprite3 z:1 tag:102];
我正在使用这个数组逻辑。我想识别这些spites并希望执行不同类型的操作。那么我的问题是如何从ccarray识别这些精灵。请用语法回答。 我在数组中随机添加了这三个精灵。现在在for循环中我想识别我有哪一个精灵。这怎么可能?
答案 0 :(得分:3)
试试这个:
for (CCSprite *spr in storeObject)
{
if (spr.tag == currentTag)
{
// you will get the current sprite
break;
}
}
答案 1 :(得分:0)
首先,拧一下额外的CCArray(另外:拧CCArray它的bug,坚持使用NSMutableArray)。你很可能完全不需要它。这将您的代码简化为:
CCSprite *sprite1=[CCSprite spriteWithFile:@"sprite1.png"];
CCSprite *sprite2=[CCSprite spriteWithFile:@"sprite2.png"];
CCSprite *sprite3=[CCSprite spriteWithFile:@"sprite3.png"];
[self addChild:sprite1 z:1 tag:100];
[self addChild:sprite2 z:1 tag:101];
[self addChild:sprite3 z:1 tag:102];
然后使用sprite3.png获取精灵只需使用getChildByTag:
CCSprite* itsSprite3 = [self getChildByTag:102];
毕竟那是标签的用途!
如果你只有3个精灵并且你没有将它们从它的部分中删除,那么如果你只是在你班级的任何地方创建3个ivars sprite1,sprite2,sprite3来访问它们就更容易了。
答案 2 :(得分:0)
为了完整性,您应该知道可以将多个子项添加到具有相同标记的对象。例如,下面的代码将提供以下日志:
for (NSUInteger i = 0; i < 10; i++) {
CCLabelAtlas *toto = [SpriteUtils mapDamageLabel:23+i];
toto.tag = 901210;
[self addChild:toto z:0 tag:toto.tag];;
}
MPLOG(@"*** remove me : before refresh %@ ***", self.soldier);
for (id child in children_) {
CCNode *ch = (CCNode *) child;
MPLOG(@"<%@> tag: %i name : %@", self.soldier, ch.tag, ch.name);
}
*现在是日志*
- [SoldierMapLayout sequenceEngagement:]: *删除我:刷新红色之前*
- [SoldierMapLayout sequenceEngagement:]:tag:1024 name:CCSprite:walk_red_rogue_idle0001.png
- [SoldierMapLayout sequenceEngagement:]:tag:891 name:CCSprite:cant move
- [SoldierMapLayout sequenceEngagement:]:tag:889 name:CCSprite:cant heal
- [SoldierMapLayout sequenceEngagement:]:tag:890名称:CCSprite:Charmed
- [SoldierMapLayout sequenceEngagement:]:tag:886 name:CCSprite:Health bar:18
- [SoldierMapLayout sequenceEngagement:]:tag:896 name:CCSprite:walk_red_rogue_cast0001.png
- [SoldierMapLayout sequenceEngagement:]:tag:895 name:CCLabelAtlas:critical damage 16
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 23
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 24
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 25
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 26
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 27
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 28
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 29
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 30
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 31
- [SoldierMapLayout sequenceEngagement:]:tag:901210 name:CCLabelAtlas:damage 32
因此,当您稍后执行[self getChildByTag:901210]时,CCNode将返回它找到的第一个带有标记的标记,而不一定是您要查找的标记。