实现 returns 节点子树中满足测试的所有节点的 passingTest

Implementing passingTest that returns all nodes in the node’s subtree that satisfy a test

我不太确定如何真正使用这个功能。我彻底搜索了互联网,但找不到使用此功能的示例实例。在 SceneKit 中,我非常熟悉按节点名称搜索的函数。但是这个函数令人困惑,因为它 returns 对 bool 的引用,它指定了一个数组。定义是这样的

func childNodes(passingTest predicate: (SCNNode, UnsafeMutablePointer<ObjCBool>) -> Bool) -> [SCNNode]

我到了这一步..这给了我所有节点我不知道为什么..

let ballToDisappear = sceneView?.scene.rootNode.childNodes(passingTest: { (node, ballYesOrNo) -> Bool in
        if (node.position.x < (maxVector?.x)! && node.position.x > (minVector?.x)!){
            if (node.position.y < (maxVector?.y)! && node.position.y > (minVector?.y)!){
                if (node.position.z < (maxVector?.z)! && node.position.z > (minVector?.z)!){
                    return ballYesOrNo.pointee.boolValue
                }
            }
        }
                    return !ballYesOrNo.pointee.boolValue
    }
    )

如有任何帮助,我们将不胜感激!谢谢!

childNodes(passingTest:) 的定义需要一个带有两个参数的闭包,return 是一个 Bool。闭包的参数是:

  • child => 要检查的节点(您称为 node
  • stop => 表示停止测试。像你那样调用这个 ballYesOrNo 本质上是令人困惑的,最好按照文档的建议将其命名为 stop

所以当且仅当您想要停止算法时,您才需要修改 stop。你绝对不想 return stop 的值直接.

要说一个子节点匹配,你必须return true,否则false。 因此:

let ballToDisappear = sceneView?.scene.rootNode.childNodes(passingTest: { (node, stop) -> Bool in
    if (node.position.x < (maxVector?.x)! && node.position.x > (minVector?.x)!){
        if (node.position.y < (maxVector?.y)! && node.position.y > (minVector?.y)!){
            if (node.position.z < (maxVector?.z)! && node.position.z > (minVector?.z)!){
                return false // or false here and true below??
            }
        }
    }
                return true // or true here, see above, depends on your logic
}
)