【问题标题】:Get properties of first and last child nodes in SKNode parent获取 SKNode 父节点中第一个和最后一个子节点的属性
【发布时间】:2015-06-08 07:56:39
【问题描述】:

如果我使用下面的方法,我可以遍历我的 SKNode 父级中的子对象,并且可以很好地获取场景中的位置

for child in platformGroup.children {
            let posInScene = self.convertPoint(child.position, fromNode: platformGroup)
            println("Pos in scene \(posInScene)")

        }

当我尝试从节点中的第一个或最后一个子节点获取位置属性时,我得到一个错误。

    let lastPlatPos = self.convertPoint(platformGroup.children.last?.position! , fromNode: platformGroup)
    println(lastPlatPos.x)

错误:可选类型“CGPoint”的值未解包,您的意思是 采用 '!'还是“?”?

我在这里错过了什么?

即使我把它包装起来,我也会得到同样的错误:

if let lastPlatPos = self.convertPoint(platformGroup.children.last?.position! , fromNode: platformGroup) {
    println(lastPlatPos.x)
}

【问题讨论】:

    标签: swift sprite-kit sknode


    【解决方案1】:

    我的错,是 ?可选的最后是问题

    let lastPlatPos = self.convertPoint(platformGroup.children.last!.position, fromNode: platformGroup)
    

    这行得通

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2011-09-07
      • 2015-08-06
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多