Search code examples
cocos2d-iphoneactioncollision-detection

Cocos2d - action in collision?


I have a collision detection and its working fine, the problem is, that whenever my "bird" is hitting a "cloud", the cloud dissapers and i get some points. The same happens for the "sol" which it should, but not with the clouds.

How can this be changed ? ive tryed a lot, but can seem to figger it out.

Collision Code

- (void)update:(ccTime)dt {
    bird.position = ccpAdd(bird.position, skyVelocity);

    NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *bird in _projectiles) {
        bird.anchorPoint = ccp(0, 0);  
        CGRect absoluteBox = CGRectMake(bird.position.x, bird.position.y, [bird boundingBox].size.width, [bird boundingBox].size.height);

        NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
        for (CCSprite *cloudSprite in _targets) {
            cloudSprite.anchorPoint = ccp(0, 0); 
            CGRect absoluteBox = CGRectMake(cloudSprite.position.x, cloudSprite.position.y, [cloudSprite boundingBox].size.width, [cloudSprite boundingBox].size.height);


            if (CGRectIntersectsRect([bird boundingBox], [cloudSprite boundingBox])) {
                [targetsToDelete addObject:cloudSprite];

            }                       
        }

        for (CCSprite *solSprite in _targets) {
            solSprite.anchorPoint = ccp(0, 0); 
            CGRect absoluteBox = CGRectMake(solSprite.position.x, solSprite.position.y, [solSprite boundingBox].size.width, [solSprite boundingBox].size.height);


            if (CGRectIntersectsRect([bird boundingBox], [solSprite boundingBox])) {
                [targetsToDelete addObject:solSprite];      
                score += 50/2;
                [scoreLabel setString:[NSString stringWithFormat:@"%d", score]];
            }                       
        }



        // NÅR SKYEN BLIVER RAMT AF FUGLEN
        for (CCSprite *cloudSprite in targetsToDelete) {
            //[_targets removeObject:cloudSprite];
            //[self removeChild:cloudSprite cleanup:YES];       
        }

        // NÅR SOLEN BLIVER RAMT AF FUGLEN
        for (CCSprite *solSprite in targetsToDelete) {
            [_targets removeObject:solSprite];
            [self removeChild:solSprite cleanup:YES];   


        }

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

    // NÅR FUGLEN BLIVER RAMT AF ALT ANDET
    for (CCSprite *bird in projectilesToDelete) {
        //[_projectiles removeObject:bird];
        //[self removeChild:bird cleanup:YES];
    }
    [projectilesToDelete release];

}

Solution

  • Well, both your cloud and sol objects are in the same array _targets. So, unless you test for proper kind in your loop, you will not be able to perform the required action on any one of them. Something like this (assuming that cloud and sol are subclasses of CCSprite):

    for (CCSprite *cloudSprite in _targets) {
        if(![cloudSprite isKindOfClass:[Cloud class]]) continue;
        cloudSprite.anchorPoint = ccp(0, 0); 
        CGRect absoluteBox = CGRectMake(cloudSprite.position.x, cloudSprite.position.y, [cloudSprite boundingBox].size.width, [cloudSprite boundingBox].size.height);
        if (CGRectIntersectsRect([bird boundingBox], [cloudSprite boundingBox])) {
            [targetsToDelete addObject:cloudSprite];
    
        }                       
    }
    

    this way you only add clouds to targetsToDelete. The key is to differentiate a cloud from a sol in your _targets. Subclassing is bullet proof. Otherwise you could implement it with the use of tags, but it could be a more fragile design. Once you can discriminate the kinds of objects in your _targets, this code can be optimized easily (fewer iterations on _targets and targetsToDelete.