How to access the object of Navigation graph in SpriteKit scene editor

喜欢而已 提交于 2019-11-30 05:21:09

In the default SpriteKit template the GameViewController has a section in the viewDidLoad function that copies over the Scene Editors Entities and Graphs.

class GameViewController: UIViewController {
    private var sceneNode: GameScene!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load 'GameScene.sks' as a GKScene.
        if let scene = GKScene(fileNamed: "GameScene") {

            // Get the SKScene from the loaded GKScene
            if let sceneNode = scene.rootNode as! GameScene? {
                self.sceneNode = sceneNode

                self.sceneNode.entities = scene.entities // <-- entities loaded here
                self.sceneNode.graphs = scene.graphs // <-- graphs loaded here

                // ... other scene loading code
            }
        }
    }
}

These Entities and Graphs array variables are declared in the GameScene. Then retrieve the graph from the array.

class GameScene : SKScene {
    var entities = [GKEntity]()
    var graphs = [String : GKGraph]()
    var navigationGraph: GKGraph<GKGraphNode>!

    override func didMove(to view: SKView) {
       self.navigationGraph = self.graphs.values.first // <-- get a reference to your graph
    }
}

If there is more than one graph in the SpriteKit Editor, then use a query to retrieve it by name.

self.navigationGraph = self.graphs.values.first(where: {$0.name == "GraphName"})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!