我在非常简单的碰撞检测方面遇到了一些麻烦。这是我目前正在使用的代码:
- (void) update:(ccTime)dt{
NSLog(@"update");
if (CGRectIntersectsRect([self.sprite boundingBox], [self.swat boundingBox])) {
NSLog(@"detected");
}}
'sprite' 和 'swat' 是 CCSprite 的子类,它们被声明为属性,使用:
@property (nonatomic, assign) CCSprite *swat;
@property (nonatomic, assign) enemyClass *sprite; //enemyClass is a subclass of CCSprite
//note that they have also been synthesized
我是否需要更改属性才能使碰撞检测正常工作?
我还尝试了以下代码:
这仅返回更新:
- (void) update:(ccTime)dt{
NSLog(@"update");
if (CGRectIntersectsRect(self.sprite.boundingBox, self.swat.boundingBox)) {
NSLog(@"detected");
}}
这会一遍又一遍地返回“检测到”,即使它们没有碰撞:
- (void) update:(ccTime)dt{
NSLog(@"update");
if (CGRectIntersectsRect(self.sprite.textureRect, self.swat.textureRect)) {
NSLog(@"detected");
}}
在两组代码中,都记录了“更新”,因此更新工作正常,只是 if 语句给我带来了问题。
如果您能给我任何解决方案来说明为什么这不起作用,或者任何其他方法让它起作用,我将不胜感激。谢谢。