0

私はゲームを構築するためにcocos2dを使用しています。CCSpriteの配列があり、それらに触れて、触れたものを削除できるようにしたいと思います。

今私はこれを持っています...

-(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]; 

    }
}

私はこれまでこれをしたことがありません。誰かが解決策を持っていますか?

ありがとう、マイケル

4

1 に答える 1

1
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]]; 
    NSMutableArray *spritesToDelete = [[NSMutableArray alloc] init];

    for(CCSprite* mySprite in myArray) {
        if (CGRectContainsPoint([mySprite boundingBox], location))
            [spritesToDelete addObject:mySprite];

    for(CCSprite* deadSprite in spritesToDelete) {
        [self removeChild:deadSprite cleanup:YES];
        [myArray removeObject:deadSprite];
    }
}

このコードは、for-eachを使用して、条件に一致するオブジェクトの配列を作成し、それらを削除します。

于 2011-11-24T03:56:54.667 に答える