いくつかの小さなスプライト (幅/高さ) がCCLayer
あり、どれがタッチされているかを検出したいと考えています。次のコードを使用します。
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGRect rect = sprite.boundingBox;
// i am doing this because of the small size of the sprites,
// so it would be more easy to detect if a sprite is taped and then move it.
if (rect.size.width > rect.size.height) {
rect.size.width *= 2.5;
rect.size.height *= 5;
rect.origin.y -= rect.size.height / 2;
} else {
rect.size.width *= 5;
rect.size.height *= 2.5;
rect.origin.x -= rect.size.width / 2;
}
CCSprite *s = nil;
for (CCSprite *sprite in [self children]) {
if (CGRectContainsPoint(rect, touchPoint)) {
s = sprite;
break;
}
}
if (s != nil) {
// do something here
}
return YES;
}
2 つのスプライトが互いに非常に近い (重なっていない) 場合を除いて、すべて正常に機能します。次に、それらの間の距離が小さいため、間違ったスプライトが検出されます。
どうすれば修正できますか?