如何在 SpriteKit 场景编辑器中访问导航图的对象 [英] How to access the object of Navigation graph in SpriteKit scene editor

查看:17
本文介绍了如何在 SpriteKit 场景编辑器中访问导航图的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 模板中,GameViewControllerviewDidLoad 函数中有一个部分可以复制场景编辑实体和图表.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆