我想让" aware“SKSpriteNode意识到触摸在节点之外(例如改变颜色)。
这些spries应该是独立的。
如何做到这一点?
谢谢
发布于 2016-10-26 14:55:12
显然,您可以实现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")https://stackoverflow.com/questions/40253284
复制相似问题