How to access navigation chart object in SpriteKit scene editor

I work with SpriteKit and draw a scene using the scene editor in xcode. According to SpriteKit, we can use a navigation graph to draw paths, and I can draw a path using navigation graphs, but I cannot access this object in quick content.

enter image description here

How to access this navigation chart object from the scene.

+5
source share
1 answer

In the default GameViewController template, the GameViewController has a section in the viewDidLoad function that copies objects and graphic scene editors.

 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 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 the SpriteKit editor has more than one graph, use the query to retrieve it by name.

 self.navigationGraph = self.graphs.values.first(where: {$0.name == "GraphName"}) 
+2
source

Source: https://habr.com/ru/post/1269287/


All Articles