我在 a 中有一些小精灵(宽度/高度),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;
}
除非两个精灵彼此非常接近(不重叠),否则一切正常。然后由于它们之间的距离很小,检测到错误的精灵。
知道我该如何纠正吗?