【问题标题】:Implementing passingTest that returns all nodes in the node’s subtree that satisfy a test实现通过测试返回节点的子树中满足测试的所有节点
【发布时间】:2018-04-23 08:37:54
【问题描述】:

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

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
    }
    )

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: ios swift boolean scenekit arkit


    【解决方案1】:

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

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

    因此,当且仅当您想停止算法时,您必须修改 stop。你永远都不想返回stop的值直接

    要说一个子节点匹配,你必须返回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
    }
    )
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多