Cocos2D - Detecting collision

Posted by Grace on Game Development See other posts from Game Development or by Grace
Published on 2012-05-31T06:43:33Z Indexed on 2012/05/31 10:51 UTC
Read the original article Hit count: 173

I am a beginner in cocos2d and im facing a problem with detecting collision for my coins. Sometimes it works sometimes it doesn't.

So basically, im creating a game which the user (ship) have to avoid the obstacles and collect coins on the way. The collision of the obstacle works well but not for the coins. I was thinking maybe the loops for creating many coins is the problem but im not sure.

Can anyone help?

My codes:

- (void)update:(ccTime)dt{
double curTime = CACurrentMediaTime();
if (curTime > _nextBridgeSpawn) {

    float randSecs = [self randomValueBetween:3.0 andValue:5.0];
    _nextBridgeSpawn = randSecs + curTime;

    float randX = [self randomValueBetween:50 andValue:500];
    float randDuration = [self randomValueBetween:8.0 andValue:10.0];

    CCSprite *bridge = [_bridge objectAtIndex:_nextBridge];

    _nextBridge++;

    if (_nextBridge >= _bridge.count) _nextBridge = 0;

    [bridge stopAllActions];
    bridge.position = ccp(winSize.width/2, winSize.height);
    bridge.visible = YES;

    [bridge runAction:[CCSequence actions:
                         [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height)],
                         [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],
                         nil]];

this is where i declare my coins (continued from the update method)

    int randCoin = [self randomValueBetween:0 andValue:5];
    _coin = [[CCArray alloc] initWithCapacity:randCoin];
    for(int i = 0; i < randCoin; ++i) {
        coin = [CCSprite spriteWithFile:@"coin.png"];
        coin.visible = NO;
        [self addChild:coin];
        [_coin addObject:coin];
    }

    float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)];
    float randCoinY = [self randomValueBetween:100 andValue:700];
    float randCoinPlace = [self randomValueBetween:30 andValue:60];
    for (int i = 0; i < _coin.count; ++i) {
        CCSprite *coin2 = [_coin objectAtIndex:i];
        coin2.position = ccp(randCoinX, (bridge.position.y + randCoinY) + (randCoinPlace *i));
        coin2.visible = YES;
        [coin2 runAction:[CCSequence actions:
                          [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height-2000)],
                          [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)],
                          nil]];  
    }

}

this is to check for collision (also in the update method)

for (CCSprite *bridge in _bridge) {        
    if (!bridge.visible) continue;

if (CGRectIntersectsRect(ship.boundingBox, bridge.boundingBox)){
            bridge.visible = NO;
            [ship runAction:[CCBlink actionWithDuration:1.0 blinks:5]];         

        }
    }
}

//this is the collision for coins which only work at times
    for (CCSprite *coin2 in _coin) { 

        if (!coin2.visible) continue;

        if (CGRectIntersectsRect(ship.boundingBox, coin2.boundingBox)) {
            NSLog(@"Coin collected");
            coin2.visible = NO;
        }

    }

}

Thank you.

© Game Development or respective owner

Related posts about collision-detection

Related posts about ios