Еще один способ сделать это - вычислить расстояние между позицией касания и вашими спрайтами. Если касание достаточно близко к одному из ваших спрайтов, вы можете убить его ... Как-то так ...

довал руководству Рэя по созданию простой игры для iPhone (здесь:http://goo.gl/fwPi) и решил, что я хочу, чтобы враги были уничтожены, когда их коснутся.

Мой первоначальный подход состоял в том, чтобы создать небольшой спрайт CCSprite в месте касания, а затем использовать CGRectMake, чтобы создать ограничивающую рамку указанного спрайта, чтобы определить, был ли затронут вражеский спрайт. Так же, как Рэй делает с снарядом / врагом. Но, конечно, мой способ сделать это не работает, и я не могу выкопать себя из этой дыры.

Вот соответствующий фрагмент кода. Любая помощь приветствуется:

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [self convertTouchToNodeSpace: touch];
    location = [[CCDirector sharedDirector] convertToGL:location];
    CCSprite *touchedarea = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(location.x, location.y, 2, 2)];
    touchedarea.tag = 2;
    [self addChild:touchedarea];
    [_touchedareas addObject:touchedarea];

}



- (void)update:(ccTime)dt {

    NSMutableArray *touchedareasToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *touchedarea in _touchedareas) {
        CGRect touchedareaRect = CGRectMake(
                                           touchedarea.position.x, 
                                           touchedarea.position.y, 
                                           touchedarea.contentSize.width, 
                                           touchedarea.contentSize.height);

        NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
        for (CCSprite *target in _targets) {
            CGRect targetRect = CGRectMake(
                                           target.position.x - (target.contentSize.width/2), 
                                           target.position.y - (target.contentSize.height/2), 
                                           target.contentSize.width, 
                                           target.contentSize.height);

            if (CGRectIntersectsRect(touchedareaRect, targetRect)) {
                [targetsToDelete addObject:target];             
            }                       
        }

        for (CCSprite *target in targetsToDelete) {
            [_targets removeObject:target];
            [self removeChild:target cleanup:YES];                                  
        }

        if (targetsToDelete.count > 0) {
            [touchedareasToDelete addObject:touchedarea];
        }
        [targetsToDelete release];
    }

    for (CCSprite *touchedarea in touchedareasToDelete) {
        [_touchedareas removeObject:touchedarea];
        [self removeChild:touchedarea cleanup:YES];
    }
    [touchedareasToDelete release];
}

Ответы на вопрос(2)

Ваш ответ на вопрос