如何在现有场景中添加精灵

时间:2016-11-24 19:28:00

标签: swift sprite-kit

在场景完全加载后添加精灵节点的最佳方法是什么?序列如下所示: 1)我构建场景,GameScene()。 2)一段时间后,我下载后端数据,并使用此信息在另一个类NodeBuilder()中构建SKSpriteNode。
3)我想将此节点添加到我已经加载的场景实例中。 实现步骤3)的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

GameScene

addChild(yourNode)

获取并保留对NodeBuilder的引用:

NodeBuilder中创建GameScene

class GameScene : SKScene {

    var nodeBuilder = NodeBuilder() // Create an instance of NodeBuilder

    func didMoveToView(skView: SKView) {

        nodeBuilder.gameScene = self // Add self as the instance of GameScene that nodeBuilder has reference to

    }
}

NodeBuilder

class NodeBuilder {

    var gameScene : GameScene! // This is how you keep your reference

    func addNodeToGameScene(node: SKNode) {

        self.gameScene.addChild(node)

    }

    addNodeToGameScene(aNode) // This is how you would call the method to add a node to GameScene from NodeBuilder
}