0

我是 Cocos2d 游戏开发的初学者,我已经完成了 raywenderlich 的教程,http ://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2 -x-tutorial 问题是我想检测怪物与玩家对象之间的碰撞,如下面的代码

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

但我无法获得这 2 个对象之间的准确命中,甚至有时这 2 个对象直接相互碰撞,但似乎日志没有运行。

4

1 回答 1

0

查看玩家和怪物创建代码会很有帮助。但我可以假设他们有不同的父母。在这种情况下,即使在视觉上它们位于同一位置,它们也可能没有相交的矩形。节点的位置与其父级相关,而不是整个场景。如果要使用世界坐标,请使用convertToWorldSpace:方法。

编辑:在评论中解决。

“您的碰撞检测在每秒仅调用一次的方法中。尝试将其移至您的 update: 方法。”

于 2013-09-16T18:25:45.007 回答