Search code examples
iossprite-kitskspritenodeskscene

iOS enumerateChildNodeswithName to access ALL physics bodies


I'm writing a helper method to display a summary of all of the physics interactions (collisions and contacts) in an SpriteKit app on iOS.

I have a simple scene, with a boundary (from self.physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)) and 3 simple shapes (2 squares and a circle) wich are added to the scene via addChild()

In my helper function, I want to search all nodes and if they have a physicsBody, print their category, collision and contactTest bit masks.

If I code the following:

enumerateChildNodesWithNeme("*") { node, _ in {
   print("Node: \(node.name)")
}

then I only get the 3 shapes listed:

Node: Optional("shape_blueSquare")
Node: Optional("shape_redCircle")
Node: Optional("shape_purpleSquare")

My scene's physicsBody is not returned. But if I use:

enumerateChildNodesWithNeme("..") { node, _ in {
   print("Node: \(node.name)")
}

then I get:

Node: Optional("shape_edge") 

If I use:

enumerateChildNodesWithNeme("..//") { node, _ in {
   print("Node: \(node.name)")
}

I get nothing, whereas I thought this would move up the node tree to the scene and then recursively return all children. The search argument "//*" also returns just the 3 children.

All the time, the node count displayed by skView.showNodecount = true is 4.

So my question is: Is there a search argument for enumerateChildNodesWithName that will return all the nodes in a scene (including the scene itself) or have I misunderstood the relationship between the scene and it's children, in that a single search cannot search both? It may be the latter, as print("\(parent.children)") returns nil, when I was expecting to see self or some variation of such.


Solution

  • This worked for me:

    enumerateChildNodesWithName("//.") { (node, _) -> Void in
         print("Node: \(node.name)")
    }
    

    About // :

    When placed at the start of the search string, this specifies that the search should begin at the root node and be performed recursively across the entire node tree.

    About . :

    Refers to the current node.

    Also this might work for you as well (if you want to find all physics bodies within a given rectangle):

    self.physicsWorld.enumerateBodiesInRect(frame) { (body, _) -> Void in
        print(body.node?.name)
    }