【问题标题】:get userData of SKSpriteNode using the scene editor使用场景编辑器获取 SKSpriteNode 的 userData
【发布时间】:2016-10-06 19:32:51
【问题描述】:

我在场景编辑器中设置了我的 SKSpriteNode 的 userData:

然后我试图得到它,使用:

let sprite:SKSpriteNode = self.childNode(withName: "sprite") as? SKSpriteNode
let numero:Int = sprite.userdata?.valueForKey("numero") as! Int

但我得到了 nil 值:“致命错误:在展开可选值时意外发现 nil”

我还尝试使用以下方法初始化 userData:

sprite.userData? = NSMutableDictionary()

但结果相同...

【问题讨论】:

  • 你保存了场景编辑器吗?
  • @Knight0fDragon 的意思是说 Xcode 中的场景编辑器中有一组众所周知的异常——它们不像 Xcode 中的其他所有东西那样自动保存。保存场景,同时在场景编辑器中(对于某些属性)以它们按预期工作的方式将它们传播到存储中......有时。

标签: swift sprite-kit skspritenode user-data


【解决方案1】:

你的精灵也可能是 nil:

尝试使用可选绑定(如果让)和 sprite 之前的“//”来搜索您的 sprite。

if let sprite = self.childNode(withName: "//sprite") as? SKSpriteNode {
       let numero:Int = sprite.userdata?.valueForKey("numero") as! Int
} else {
   print("I cannot find sprite..")
}

它从当前位置执行递归搜索。 检查 API 参考 here。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 2019-06-28
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多