默認情況下在cocos2d一個CCSprite的anchorpoint是CCP(0.5,0.5),其修改子畫面的位置的比較。 比較位置以檢查遊戲循環中精靈之間的相對距離,我認爲相等運算符不是最佳選擇,大於/小於等於更好地準備不同的位置情況,精靈移動步驟。
爲了更好地理解我包括例如精靈情況:
![enter image description here](https://i.stack.imgur.com/N6wAF.png)
要檢查的相對位置(檢測一個精靈是在其他精靈何邊),你有不同的選擇,程序您的碰撞檢測檢查:
1)
if (CGRectIntersectsRect(sprite1.boundingBox, sprite2.boundingBox)) {
CGPoint diff = ccpSub(sprite1.position, sprite2.position)
if (diff.x<=0) {
// sprite1 is on the left side
} else {
// sprite1 is on the right side
}
}
2)
if (CGRectIntersectsRect(sprite1.boundingBox, sprite2.boundingBox)) {
if (sprite1.position.x <= sprite2.position.x) {
// sprite1 is on the left side
} else {
// sprite1 is on the right side
}
}
來源
2013-04-08 09:21:02
nzs
檢測sprite影響X和Y位置。 – Tirth 2013-04-08 07:15:53
@i人我試過但知道要編碼它 – KsK 2013-04-08 07:21:16