如何在SpriteKit场景编辑器中访问导航图的对象 [英] How to access the object of Navigation graph in SpriteKit scene editor
问题描述
我正在使用SpriteKit并在xcode中使用场景编辑器绘制场景。根据SpriteKit,我们可以使用导航图来绘制路径,我可以使用导航图绘制路径,但我无法在swift后端访问此对象。
I am working with SpriteKit and draw a scene by the help of the scene editor in xcode. According to SpriteKit, we can use Navigation graph to draw the paths and I am able to draw a path using navigation graph but i am not able to access this object in swift back-end.
如何从场景中访问此导航图形对象。
How to access this Navigation graph object from the scene.
推荐答案
在默认的SpriteKit模板中, GameViewController
在 viewDidLoad中有一个部分
复制场景编辑器实体和图形的功能。
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
}
}
}
}
这些实体和图形数组变量在GameScene中声明。然后从数组中检索图形。
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
}
}
如果SpriteKit编辑器中有多个图形,则使用查询按名称检索它。
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"})
这篇关于如何在SpriteKit场景编辑器中访问导航图的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!