1
votes

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?

2

2 Answers

6
votes

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!

0
votes

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.