Iphone 使用Cocos2d碰撞检测时如何引用精灵?
使用下面的方法,在检查特定精灵是否相交时,如何引用它们Iphone 使用Cocos2d碰撞检测时如何引用精灵?,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,使用下面的方法,在检查特定精灵是否相交时,如何引用它们 - (void)update:(ccTime)dt { for (CCSprite *sprite in movableSprites) { if (CGRectIntersectsRect(sprite.boundingBox, sprite.boundingBox)) { break; } } } 似乎所有精灵都可以在moveableSprites对象中使用,但我
- (void)update:(ccTime)dt {
for (CCSprite *sprite in movableSprites) {
if (CGRectIntersectsRect(sprite.boundingBox, sprite.boundingBox)) {
break;
}
}
}
似乎所有精灵都可以在moveableSprites对象中使用,但我不知道如何检查特定精灵是否发生碰撞。。。我不知道该怎么称呼他们。如果有一种更简单的方法来执行碰撞检测,我很感兴趣。上面的代码似乎总是会返回TRUE,因为您正在检查精灵的边界框是否与精灵发生碰撞,因为它们是相同的,所以总是会发生碰撞
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]可以代替sprite了
您可以只添加boundingBox来检查碰撞,如下所示
if (CGRectIntersectsRect([self getChildByTag:2].boundingBox, checkSprite.boundingBox)) {//
break;
}
上面的代码似乎总是返回TRUE,因为您正在检查精灵的边界框是否与精灵冲突,并且由于它们是相同的,所以总是会发生冲突
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]可以代替sprite了
您可以只添加boundingBox来检查碰撞,如下所示
if (CGRectIntersectsRect([self getChildByTag:2].boundingBox, checkSprite.boundingBox)) {//
break;
}