Looping through an array to remove a touched object (iPhone/Cocos2d)

Posted by Michael Lowe on Stack Overflow See other posts from Stack Overflow or by Michael Lowe
Published on 2011-11-24T02:34:32Z Indexed on 2011/11/24 17:51 UTC
Read the original article Hit count: 292

Filed under:
|
|
|

I am using cocos2d to build a game. I have an array of CCSprites and I want to be able to touch them and delete the one that was touched.

Right now I have this...

-(void) spawn {
   mySprite = [CCSprite spriteWithFile:@"image.png"];
   mySprite.position = ccp(positionX,positionY);
   [myArray addObject:mySprite];
   [self addChild:mySprite];
}
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch* touch = [touches anyObject];
   CGPoint location = [touch locationInView: [touch view]]; 

    NSUInteger i, count = [myArray count];
    for (i = 0; i < count; i++) {
    mySprite = (CCSprite *)[myArray objectAtIndex:i];
    if (CGRectContainsPoint([mySprite boundingBox], location)) {

       [self removeChild:mySprite cleanup:YES]; 

    }
}

I have never done this before. Does anyone have a solution?

Thanks, Michael

© Stack Overflow or respective owner

Related posts about iphone

Related posts about arrays