Search code examples
collision-detectioncocos2d-x

Collision in Cocos2d


I am beginner in Game Development in Cocos2d, and I have been through a tutorial of raywenderlich,http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial The problem is that i want to detect a collision between the monster with the player object like the following code

-(void)gameLogic:(ccTime)dt {
    [self addMonster];
    for (CCSprite *monster in _monsters) {

            CGRect targetRect = CGRectMake(
                                           monster.position.x - (monster.contentSize.width/2),
                                           monster.position.y - (monster.contentSize.height/2),
                                           monster.contentSize.width,
                                           monster.contentSize.height);
            if (CGRectIntersectsRect(targetRect, player.boundingBox)) {
                NSLog(@"Intersect Right!");
            }

    }
} 

-(id) init
{
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background-music-aac.caf"];
    _monsters = [[NSMutableArray alloc] init];
    _projectiles = [[NSMutableArray alloc] init];
    if ((self = [super initWithColor:ccc4(255, 255, 255, 255)])) {
        CGSize winSize = [CCDirector sharedDirector].winSize;
        player = [CCSprite spriteWithFile:@"player.png"];
        [player setColor:ccRED];
        player.position = ccp(player.contentSize.width/2, winSize.height/2);
        [self addChild:player];
    }
    [self schedule:@selector(gameLogic:) interval:1.0];
    [self schedule:@selector(update:)];
    [self setTouchEnabled:YES];
    return self;
}

- (void) addMonster {
    CCSprite * monster = [CCSprite spriteWithFile:@"monster.png"];

    // Determine where to spawn the monster along the Y axis
    CGSize winSize = [CCDirector sharedDirector].winSize;
    int minY = monster.contentSize.height / 2;
    int maxY = winSize.height - monster.contentSize.height/2;
    int rangeY = maxY - minY;
    int actualY = (arc4random() % rangeY) + minY;

    // Create the monster slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above

    monster.position = ccp(winSize.width + monster.contentSize.width/2, actualY);
    [self addChild:monster];

    // Determine speed of the monster
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;

    // Create the actions
    CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
                                                position:ccp(-monster.contentSize.width/2, actualY)];
    CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
//        CCScene *gameOver = [GameOverLayer sceneWithWon:NO];
//        [[CCDirector sharedDirector] replaceScene:gameOver];
        [node removeFromParentAndCleanup:YES];
    }];
    [monster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
    monster.tag = 1;
    [_monsters addObject:monster];
}

But i can't get the accurate hit between those 2 object and even sometimes those 2 object hit each other directly but it's seem log wasn't run.


Solution

  • It can be helpful to see player and monster creation code. But I can assume that they have different parents. In this case they can have not intersected rects even if visually they are at the same position. Node's position is relatieve to it's parent, not the whole scene. If you want to use world coordinates, use convertToWorldSpace: method.

    EDIT: Solved in comments.

    "Your collision detection is in the method that is called only once a second. Try to move it to your update: method instead."