Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You keep your objects in the same list (_targets). Therefore the game can not decide if you are talking about a cloud or about a sol, both are just CCSprites for the game. Therefore it loops through every element, handling it as a cloud and then does the same thing again, handling the objects a sol.

To prevent this save the objects into 2 differnt lists: Clouds into _cloudTargets and the sol into _solTargets and change the two loops to:

for (CCSprite *cloudSprite in _cloudTargets) {
      [..]   

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

        }                       
    }

    for (CCSprite *solSprite in _solTargets) {
       [..]    

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

This should work fine.

Hope this helps

floAr

share|improve this answer
    
Thanks a lot! im gonna try this when i get home :-) –  PatrickR Oct 2 '12 at 12:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.