我想让 SKSpriteNode“意识到”触摸在节点之外。(例如改变颜色)
如果可能的话,我想使用 tuocesMoved(节点外)
我不想以任何其他方式连接它们。 这些spries应该是独立的。
怎么做?
谢谢
最佳答案
显然,您可以实现 touchesBegan
方法来处理触摸事件:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, with: event)
}
用当前 Swift 3 语法处理触摸的其他事件是:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}
关于这一点,如果您不想子类化您的 SKSpriteNode
以添加有用的属性,您可以使用 userData
属性来存储有关其他节点或他自己的信息以及还有关于当前上下文:
yourSprite.userData = NSMutableDictionary()
yourSprite.userData?.setValue(SKColor.blue, forKeyPath: "currentColor")
yourSprite.userData?.setValue(true, forKeyPath: "selected")
关于swift - SKSprite节点链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253284/