open func nodes(at p: CGPoint) -> [SKNode]
可用于检测您已触摸某个点的哪些节点。在此之后,您可以排除CGRect
区为您希望每个节点..
import SpriteKit
class GameScene: SKScene {
var warningZone = CGRect(x: -80, y: -60, width: 100, height: 100)
override func didMove(to view: SKView) {
let nodeRed = SKSpriteNode.init(color: .red, size: CGSize(width:300,height:200))
nodeRed.name = "nodeRed"
addChild(nodeRed)
nodeRed.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
let nodeBlue = SKSpriteNode.init(color: .blue, size: CGSize(width:300,height:200))
nodeBlue.name = "nodeBlue"
addChild(nodeBlue)
nodeBlue.position = CGPoint(x:self.frame.midX+100,y:self.frame.midY+20)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let nodesTouched = self.nodes(at: location)
for node in nodesTouched {
guard let n = node.name else { return }
print("Node touched: \(node.name) at point:\(location)")
switch n {
case "nodeRed":
// do your exclusions for the nodeRed
print("\(node.frame)")
if !warningZone.contains(location) {
print("you have touch a safe red zone")
}
case "nodeBlue":
// do your exclusions for the nodeBlue
print("\(node.frame)")
default:
break
}
}
}
}
}
输出(我画一个白色矩形warningZone只能告诉你在哪儿呢..):
谢谢你的回答,但我认为这是我寻求的解决方案非常淘气的解决方法。这种方法需要手动检查场景中的每个spripte :(想象一下场景中有几十个或者几个精灵 - 这个开关会有多混乱。我正在寻找解决方案,您可以在其中启用特定sprite的用户交互并覆盖它的命中测试方法。这种方法允许你对SKSpriteNode进行子类化处理,并且充分处理子类定义中子类的类型。 – ukaszm
那么,你提出的这种做法实际上是不可能的,因为你可以触摸一个不直接知道你想要触摸的节点的节点,因为你是在这个子类中,或者它知道他的父节点,但父母不知道正确的节点。换句话说,如果你在堆上拍摄,你必须知道所有堆:在这种情况下,你的触摸就像一颗子弹。 –