如何检测cocos2d中sprite的碰撞

KsK*_*KsK -1 iphone cocos2d-iphone ios ccsprite

我想检测精灵与另一个精灵的碰撞.但我想检查精灵是否被其他精灵的左侧部分触摸或与精灵的右侧部分接触.我使用下面的代码来检测碰撞.但是这会检测到整体碰撞(精灵在其他精灵的任何一点都被触摸过).但如果精灵被触摸到其他精灵的正面,则想要计数为碰撞.

if (CGRectIntersectsRect(sprite1.boundingBox, sprite2.boundingBox)) {
            NSLog(@"sprite1 to delete");
 }
Run Code Online (Sandbox Code Playgroud)

有人知道怎么做吗?

jye*_*yek 5

你能比较碰撞时精灵的位置吗?

假设sprite1.anchorpoint,sprite2.anchorpoint = ccp(0,0).检测到碰撞时:

// left part of sprite 1 touched right part of sprite 2
if (sprite1.position.x == sprite2.position.x + sprite2.contentSize.width.x) {

}
// right part of sprite 1 touched left part of sprite 2
else if (sprite1.position.x + sprite1.contentSize.width.x == sprite2.position.x) {

}
Run Code Online (Sandbox Code Playgroud)