Hi, I am a new learning app developer, so I'm sorry if this question is silly, but I have a .sks file that contains several TileMapNodes, and I want to add an edge physics body so that my player character sprite cannot leave the screen. Is there any way for me to access the Nodes I have already created from my GameScene.swift file so I can add a physics body to those Nodes? If not, are there any suggestions for accomplishing what I want to do?
How do I access Nodes created in .sks file from a .swift file?
The scene has a childNode function you can use to retrieve a node from the .sks file. Supply the name of the node to the function. You can find a node's name in the .sks file.
The childNode function returns a generic SKNode so you have to cast it to the type of node you want. In your examle you will have to cast it to SKTileMapNode to get a tile map node.
let tileMapNode = childNode(withName: "nameOfYourTileNode") as? SKTileMapNode
So the way to get node from scene is simple called a func called childNode
And there's a better way to add physics to scene bounds, try follow this guide on documentation.
Code Block Swift let nodeFromScene = childNode(withName: "name of your node")
And there's a better way to add physics to scene bounds, try follow this guide on documentation.