5 Replies
      Latest reply on Nov 29, 2019 1:07 PM by Zoblod
      Zoblod Level 1 Level 1 (0 points)

        Ive put a node into the SKScene file and tried to find it in code with childNode(withName: String) and it refuses to find it.

         

        import GameKit

        import SpriteKit

        import Foundation

         

        class LoginScene:SKScene{

         

            override func didMove(to view: SKView) {

                let buttons = childNode(withName: "//button")

                buttons.name = "button"

                backgroundColor = SKColor.black

                self.addChild(buttons)

             

            }

         

         

        }// end of LoginScene class

         

        Please help me get some insight into this problem. i keep getting a nill.

        • Re: Cant find Child node in SKScene
          szymczyk Level 3 Level 3 (100 points)

          What is the name of the node in the SKS file? If the name is not //button, the buttons constant will be nil in the code you supplied.

           

          The name you supply in the childNode function has to match the name in the SKS file. My guess is the two slashes should not be there.

            • Re: Cant find Child node in SKScene
              Zoblod Level 1 Level 1 (0 points)

              //

              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. Otherwise, it performs a recursive search from its current position.

               

              I got it from this website https://developer.apple.com/documentation/spritekit/sknode/searching_the_node_tree

               

              the name of the node is button in the skscene and its not working and i have tried it without the slashes.

               

              what im trying to do is create buttons in the skscene then connect it to code instead of vice versa.

                • Re: Cant find Child node in SKScene
                  bg2b Level 1 Level 1 (0 points)

                  Maybe just iterate through everything in the scene's children field and print out names, at least for debugging.  (The code as written might well crash if it had found the button, since SpriteKit doesn't like addChild when the added node already has a parent.)

                    • Re: Cant find Child node in SKScene
                      Zoblod Level 1 Level 1 (0 points)

                      I firgured out the problem, dang im stupid. But pretty much i had to go to the sks scene file and change the custom class from SKScene to LoginScene so that thre code will link with that class and that when i do self.findChild it will go in the scene to find it. Thats why it wasnt finding it because they werent linked.