OGeek|极客世界-中国程序员成长平台

标题: ios - SpriteKit : how to iterate through ancestors of a node? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:09
标题: ios - SpriteKit : how to iterate through ancestors of a node?

我们要遍历节点的祖先,直到找到具有特定类的父节点。

SpriteKit 允许您使用 children 属性遍历子级,但 parent 属性只包含直接父级——而不是父级数组。我们如何遍历一个节点的所有祖先?



Best Answer-推荐答案


我不知道有什么函数可以让您在层次结构中向上移动,类似于 enumerateChildNodes 函数允许您在层次结构中向下移动。也许这个递归函数可能会有所帮助。下面我将递归设置为在没有父级或父级是 SKScene 类时结束。您可能需要对其进行调整,以便在找到您的特定类时结束递归。

func parentNodesOf(_ node: SKNode) -> [SKNode]
{
    if let parentNode = node.parent
    {
        if parentNode is SKScene
        {
            return []
        }
        else
        {
            return [parentNode] + parentNodesOf(parentNode)
        }
    }
    else
    {
        return []
    }
}

希望这对您有所帮助!

关于ios - SpriteKit : how to iterate through ancestors of a node?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43465786/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4