检测 Touch 是否在 Sprite Bounds 内,但忽略 Children
Detect if Touch is Within Sprite Bounds, but Ignore Children
我有一个 sprite,我需要检测它的范围内是否有触摸。通常只使用下面的代码就可以了,但是我当前的精灵有 children 超出了精灵的范围。正因为如此,我的触摸被检测到 parent 边界之外,因为精灵边界似乎包括它的边界 children。有谁知道如何检测是否仅在 parent 上进行了触摸(而不是 children )?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
var touchLocation = touch.locationInNode(self)
if mySprite.containsPoint(touchLocation) {
// Do Something
}
}
尝试检查 touchLocation
是否在 mySprite
的范围内,如下所示:
if CGRectContainsPoint(mySprite.frame, touchLocation) {
// Do something
}
我有一个 sprite,我需要检测它的范围内是否有触摸。通常只使用下面的代码就可以了,但是我当前的精灵有 children 超出了精灵的范围。正因为如此,我的触摸被检测到 parent 边界之外,因为精灵边界似乎包括它的边界 children。有谁知道如何检测是否仅在 parent 上进行了触摸(而不是 children )?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first as UITouch!
var touchLocation = touch.locationInNode(self)
if mySprite.containsPoint(touchLocation) {
// Do Something
}
}
尝试检查 touchLocation
是否在 mySprite
的范围内,如下所示:
if CGRectContainsPoint(mySprite.frame, touchLocation) {
// Do something
}