2011-04-28 146 views
1

我得到了2个精灵。我使用边界框来检查与CGRectIntersectsRect的冲突。但它不起作用。 HBBall和HBpaddle有一个名为image的CCSprite。cocos2d精灵碰撞检测包围盒

初始化:

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 retuns总是如此。有人有想法吗?

回答

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); 

进行必要的调整!

希望能帮助!

+0

是的,这是正确的做法。 +1 – tallen11 2011-04-30 13:49:18

+0

一个小问题,但不是s.position.x和s.position.y指向精灵的中心?我想你应该使用: CGRect absoluteBox = CGRectMake([s boundingBox] .origin.x,[s boundingBox] .origin.y,[s boundingBox] .size.width ,[s boundingBox] .size.height); HTH – Ted 2012-02-24 11:25:17

+0

取决于您的定位点。 – renevdkooi 2012-04-05 05:50:36