swift - SKSprite节点链

标签 swift xcode sprite-kit skspritenode sknode

我想让 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/

相关文章:

ios - Swift iOS Playground : Error sending deferral props

ios - SKScene didChangeSize : called multiple times

ios - 快速排序 NSArray

ios - 访问另一个类中定义的枚举值

swift - EXC_BAD_INSTRUCTION(代码=EXC_1386_INBOP,子代码=0x0

ios - 在 ios 通用设备上构建失败,但在模拟器上构建正常

swift - UIRefreshControl 故障与自定义 TableViewCell 相结合

ios - 构建时间段的数据模型

iphone - Ios加密。?

ios - 等待方法完成以启动 objective-c 中的下一个方法