We want to iterate through the ancestors of a node until we find a parent with a specific class.
SpriteKit lets you iterate through children with the children
property, but the parent
property only holds the immediate parent -- not an array of parents. How do we iterate through all the ancestors of a node?
I'm not aware of a function that lets you go up the hierarchy, similar like the enumerateChildNodes function allows you to go down the hierarchy. Maybe this recursive function may help. Below I've set recursion to end when there is no parent, or when the parent is an SKScene class. You may need to adjust it so that recursion ends when your particular class is found.
func parentNodesOf(_ node: SKNode) -> [SKNode]
{
if let parentNode = node.parent
{
if parentNode is SKScene
{
return []
}
else
{
return [parentNode] + parentNodesOf(parentNode)
}
}
else
{
return []
}
}
Hope this helps!