开发者

cocos2d sprite collision detection boundingbox

开发者 https://www.devze.com 2023-03-01 04:00 出处:网络
I got 2 sprites. I use the boundingbox to check for collision with CGRectIntersectsRect. But it is not working.

I got 2 sprites. I use the boundingbox to check for collision with CGRectIntersectsRect. But it is not working. HBBall and HBpaddle has a CCSprite called image.

Init:

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

Update:

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

CGRectIntersectsRect retuns always true. Does anyone have an idea?


you cant pass directly the bounding box, because it's relative to the sprite. You MUST pass the absolute CGRect boundingbox like this:

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

make necessary adjustments!

hope can help!


http://www.iphonedevsdk.com/forum/iphone-sdk-game-development/17082-cocos2d-collision-detection-between-sprites.html ? Have you googled? This seems like it would be a pretty basic issue in the cocos2d framework.

0

精彩评论

暂无评论...
验证码 换一张
取 消