Search code examples
cocos2d-iphonecollision-detectionsprite

Collision Checking Issue


So in my game I have items dropping from the top of the screen, and when the player (another sprite) catches the items, the item goes away and adds one to the counter.

This is my Collision Checking Method

//WHEN THE THINGS COLLIDE, THEY DISSAPEAR
- (void)update:(ccTime)dt {
CGSize winSize = [[CCDirector sharedDirector] winSize];

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);
    BOOL playerHit = FALSE;
        CGRect playerRect = CGRectMake(
                                       _Banker.position.x - (_Banker.contentSize.width/2),
                                       _Banker.position.y -     (_Banker.contentSize.height/2),
                                       _Banker.contentSize.width,
                                       _Banker.contentSize.height);

        if (CGRectIntersectsRect(playerRect, targetRect)) {
            //[targetsToDelete addObject:target];
            playerHit = TRUE;
                [targetsToDelete addObject:target];

            break;

    }

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

        _targetsDestroyed++;
        [_label setString:[NSString stringWithFormat:@""]];
        if (_targetsDestroyed > 30) {
            GameWinScene *gameWinScene = [GameWinScene node];
            _targetsDestroyed = 0;
            [[CCDirector sharedDirector] replaceScene:gameWinScene];
        } else{
            NSString *killcounttext = [NSString stringWithFormat:@"Catches: %i",     _targetsDestroyed];
            self.label = [CCLabelTTF labelWithString:killcounttext fontName:@"Zapfino"     fontSize:20];
            _label.color = ccc3(225,225,225);
            _label.position = ccp(winSize.width * 0.20,winSize.height * 0.92);
            [self addChild:_label];
        }
    }

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

I added a CCLOG to make sure that the target was being added to targetstodelete because in my next method i then delete anything in targetstodelete. The CCLOG confirmed that the targets ARE being added but they are not being deleted.

This is my deleting method

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

        _targetsDestroyed++;
        [_label setString:[NSString stringWithFormat:@""]];
        if (_targetsDestroyed > 30) {
            GameWinScene *gameWinScene = [GameWinScene node];
            _targetsDestroyed = 0;
            [[CCDirector sharedDirector] replaceScene:gameWinScene];
        } else{
            NSString *killcounttext = [NSString stringWithFormat:@"Catches: %i", _targetsDestroyed];
            self.label = [CCLabelTTF labelWithString:killcounttext fontName:@"Zapfino" fontSize:20];
            _label.color = ccc3(225,225,225);
            _label.position = ccp(winSize.width * 0.23,winSize.height * 0.92);
            [self addChild:_label];
        }
    }

Any help is appreciated but please don't just say "Go learn objective-C" Like some people do :/


Solution

  • In your code you written this lines,

     if (targetsToDelete.count > 0) {
            [targetsToDelete addObject:target];
        }
    

    I can't understand why you add this, so i think there is mistake so please check otherwise explain why you add this code..