Objective c 从ccarray cocos2d中识别精灵

Objective c 从ccarray cocos2d中识别精灵,objective-c,cocos2d-iphone,ios6,Objective C,Cocos2d Iphone,Ios6,我在ccarray中添加了三种雪碧 CCArray *storeObject=[[CCArray alloc]initWithCapacity:10]; CCSprite *sprite1=[CCSprite spriteWithFile:@"sprite1.png"]; CCSprite *sprite2=[CCSprite spriteWithFile:@"sprite2.png"]; CCSprite *sprite3=[CCSprite spriteWithFil

我在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];
我使用这个数组作为逻辑。我想识别这些唾液并执行不同类型的操作。那么我的问题是,我怎样才能从ccarray中识别出这些精灵呢。请用语法回答。 我在数组中随机添加了这三个精灵。现在在for循环中,我想确定我有哪一个精灵。这怎么可能呢?

试试这个:

for (CCSprite *spr in storeObject)
{
     if (spr.tag == currentTag)
     {
         // you will get the current sprite
         break;
     }
}

首先,拧掉那个额外的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精灵1、精灵2、精灵3,以便在类中的任何位置随时访问它们就更容易了。

为了完整性,您应该注意,您可以向具有相同标记的对象添加多个子对象。例如,下面的代码将在以下时间之后提供日志:

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);
}
for(整数i=0;i<10;i++){
CCLABELALAS*toto=[SpriteUtils mapDamageLabel:23+i];
toto.tag=901210;
[self addChild:toto z:0标记:toto.tag];;
}
MPLOG(@“***删除我:刷新前%@***”,self.soldier);
for(儿童中的儿童id){
CCNode*ch=(CCNode*)子节点;
MPLOG(@“标记:%i name:%@”,self.soldier,ch.tag,ch.name);
}
*现在查看日志*

-[SoldierMalayout SequenceEngament::*删除我:刷新前红色*

-[SoldierMalayout SequenceEngament:]:标签:1024姓名:CCSprite:walk\u red\u rogue\u idle001.png

-[SoldierMalayout SequenceEngament:]:标签:891姓名:CCSprite:无法移动

-[SoldierMalayout sequenceEngagement:]:标签:889姓名:CCSprite:无法治疗

-[SoldierMalayout sequenceEngagement::]:标签:890姓名:CCSprite:Charmed

-[SoldierMalayout sequenceEngagement:]:标签:886姓名:CCSprite:健康栏:18

-[SoldierMalayout SequenceEngament:]:标签:896姓名:CCSprite:walk\u red\u rogue\u cast0001.png

-[士兵军演顺序交战:]:标签:895姓名:CCLABELALAS:致命伤害16

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害23

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害24

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害25

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害26

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害27

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害28

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害29

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害30

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害31

-[士兵军演顺序交战:]:标签:901210姓名:CCLABELALAS:伤害32


因此,当您稍后执行[self-getChildByTag:901210]时,CCNode将返回它找到的第一个带有标记的文件,不一定是您要找的精灵。

您可以将精灵标签与数组索引no???@Anusha关联起来,我知道,但是如果我随机填充这些精灵并想检测其中一个。那么我如何才能确定我有哪一个精灵。然后您必须在循环中检查每个精灵标签,以获得匹配的精灵标签。谢谢回复。我用的是这样的语法,但我不知道它是对还是错。对于(int i=0;i)您可以尝试下面的代码,或者更改代码,如:value.tag==100。此外,您还必须强制转换[storeObject objectAtIndex:i]感谢CCSprite。非常感谢。它很有效。我使用value.tag,它给了我一个正确的答案。感谢你的回复。实际上我有三个sprite,我想将它随机存储在一个至少10个容量的数组中。现在我想从该数组中获取值,并想确定我拥有的是哪一个sprite。