使用下面的方法,在检查它们是否相交时如何引用特定的精灵?
- (void)update:(ccTime)dt {
for (CCSprite *sprite in movableSprites) {
if (CGRectIntersectsRect(sprite.boundingBox, sprite.boundingBox)) {
break;
}
}
}
似乎所有精灵都在moveableSprites对象中可用,但我不知道如何检查特定精灵是否发生碰撞......我不知道如何引用它们。如果有一种更简单的方法来执行碰撞检测,我很感兴趣。
答案 0 :(得分:3)
上面的代码似乎总是返回TRUE,因为您正在检查精灵的边界框是否与精灵发生碰撞,并且因为它们始终是相同的。< / p>
if (CGRectIntersectsRect(sprite.boundingBox, sprite.boundingBox)) {//
break;
}
应该与不同的精灵比较不同的精灵。
if (CGRectIntersectsRect(sprite.boundingBox, otherSprite.boundingBox)) {//
break;
}
如果这不能回答你的问题,你可能希望避免枚举数组?如果是这种情况,请尝试使用标记。如下所示。
CCSprite *aSprite = [CCSprite spriteWithFile:@"hurdle1.png"];
[self addChild:aSprite tag:2];
现在[self getChildByTag:2]可以取代精灵 你可以添加boundingBox来检查碰撞,如下所示。
if (CGRectIntersectsRect([self getChildByTag:2].boundingBox, checkSprite.boundingBox)) {//
break;
}