Cant find Child node in SKScene

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.

Answered by Zoblod in 396286022

false alert.


I can remove the node from the parent but i cant access it to do anything if i do childnode with name. position it wont work.

But if i do childNode with name. remove from parent it works like *** lol

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.

//

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.

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

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.

Accepted Answer

false alert.


I can remove the node from the parent but i cant access it to do anything if i do childnode with name. position it wont work.

But if i do childNode with name. remove from parent it works like *** lol

Cant find Child node in SKScene
 
 
Q