我试图在SKSpriteNode上触发触摸事件,其中点击了其子节点。当它触摸的子节点时,事件不会被触发。我发现使用.parent
进行黑客攻击,但并不觉得最有效或最优雅的做法是。
请参阅以下代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node is PlanListItem || node.parent is PlanListItem {
for plan in planListItems as [PlanListItem] {
plan.selected = false
}
// Some more code...
}
}
非常感谢。
答案 0 :(得分:3)
您可以在节点子类中执行此操作:
class PlanListItem:SKSpriteNode {
var isSelected: Bool = false
override init(texture size etc) {
//your init
self.userInteractionEnabled = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("PlanListItem touched")
isSelected = !isSelected
}
}