本文介绍了cocos2d sprite碰撞检测边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个sprites。我使用边界框来检查与CGRectIntersectsRect的冲突。但它不工作。
HBBall和HBpaddle有一个CCSprite称为image。

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

更新:

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

CGRectIntersectsRect retuns始终为true。有没有人有一个想法?

CGRectIntersectsRect retuns always true. Does anyone have an idea?

推荐答案

你不能直接传递边界框,因为它是相对于精灵。你必须这样传递绝对的CGRect边界框:

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

进行必要的调整!

帮助!

这篇关于cocos2d sprite碰撞检测边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 06:31