Search code examples
iphonecocos2d-iphonedetectionsprite

sprite detection problem


I have two pair of sprites on the screen.when two sprites are clicked i want to check if the clicked two sprites are same or not,if they are same then remove from screen.can anyone please give me any suggestions to do this. thanks.

this is the code i have done so far..

NSString *name = [NSString stringWithFormat:@"gimg.png"];   
CCTexture2D * texture = [[CCTextureCache sharedTextureCache] addImage:name];

NSMutableArray *imgFrameTemp = [NSMutableArray array];

for (int i = 0; i <2; i++) {
    CCSpriteFrame *imgFrame1 = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(i*50, 0*50, 50, 50)];
CCSpriteFrame *imgFrame2 = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(i*50, 0*50, 50, 50)];

[imgFrameTemp addObject:imgFrame1];
[imgFrameTemp addObject:imgFrame2];
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
    int ran = arc4random()%[imgFrameTemp count];
    CCSpriteFrame * img = [imgFrameTemp objectAtIndex:ran];
    CCSprite *sprite = [CCSprite spriteWithSpriteFrame:img];
    sprite.anchorPoint = ccp(0,0);
    sprite.position = ccp(i*60,(j+1)*60);
    [self addChild:sprite];
    [imgFrameTemp removeObjectAtIndex:ran];

}

}

Now my Four Sprites are on the screen, i want to check if two same sprites are clicked and remove them.


Solution

  • do u want to check two sprite image is same?

    if it is correct "CCSprite does not provide image name so u set manually ".CCSprite have property named "userData" .

    //get touch sprites user Data
            NSString *str=spr.userData;
            Nsstring *str1=spr1.userData;
            if ([srt isEqualToString:str1) 
            {
                CCLOG(@"two sprites are same");
            }