cocos2d精灵碰撞检测边界框

时间:2011-04-28 16:43:16

标签: iphone cocos2d-iphone collision-detection

我有2个精灵。我使用boundingbox检查与CGRectIntersectsRect的冲突。但它没有用。 HBBall和HBpaddle有一个名为image的CCSprite。

初​​始化:

    ball = [[HBBall alloc] init];
    ball.position = ccp(150, 50);
    [self addChild:ball];
    [update addObject:ball];

    paddle1 = [[HBPaddle alloc] init];
    paddle1.position = ccp(50, 160);
    [self addChild:paddle1];

更新

if (CGRectIntersectsRect([paddle1.image boundingBox], [ball.image boundingBox])) 
    CCLOG(@"ball hit paddle");

CGRectIntersectsRect总是返回true。有没有人有想法?

2 个答案:

答案 0 :(得分:6)

你不能直接传递边界框,因为它与精灵有关。你必须传递绝对的CGRect边界框,如下所示:

s = CCsprite
s.anchorPoint = ccp(0, 0);    
CGRect absoluteBox = CGRectMake(s.position.x, s.position.y, [s boundingBox].size.width, [s boundingBox].size.height);

进行必要的调整!

希望能帮到你!

答案 1 :(得分:0)

http://www.iphonedevsdk.com/forum/iphone-sdk-game-development/17082-cocos2d-collision-detection-between-sprites.html?你用Google搜索了吗?这似乎是cocos2d框架中一个非常基本的问题。