【问题标题】:SKSpriteNode chainSKSpriteNode 链
【发布时间】:2016-10-26 03:36:13
【问题描述】:

我想让 SKSpriteNode 意识到触摸在节点之外。(例如改变颜色)

如果可能的话,我想使用 tuochesMoved(节点外)

我不想以任何其他方式连接它们。 那些 spries 应该是独立的。

怎么做?

谢谢

【问题讨论】:

  • 可能有几种方法可以做到这一点,但我立即想到的一种方法是 Swift 的 didSet 和 willSet 属性观察器。您可以在被触摸的对象内部创建一个布尔变量,该变量本身具有 didSet 或 willSet(当发生更改时激活的函数,您将在触摸对象的 touchesBegan 方法中进行的更改),这可以告诉另一个精灵关于触摸。但它需要有它的细节,所以这有点混乱......我不是专家。
  • 对不起,我想获取touchesMove节点外的信息。你可以帮帮我吗?谢谢

标签: swift xcode sprite-kit skspritenode sknode


【解决方案1】:

显然你可以实现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")

【讨论】:

  • 对不起,我想获取touchesMove节点外的信息。你可以帮帮我吗?谢谢
  • 您应该添加一个新问题,然后将新链接发送给我。我很乐意为您提供帮助。
  • ? 的第一个参数touchesCancelled 很重要,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多