7

I have the following code (SceneKit in Swift targeting iOS):

let scnView = self.view as SCNView

let scene = SCNScene()

let levelScene = SCNScene(named: "level")
scene.rootNode.addChildNode(levelScene.rootNode)

scnView.scene = scene
scnView.backgroundColor = UIColor.grayColor()

scnView.allowsCameraControl = true
scnView.showsStatistics = true

The problem is, at scene.rootNode.addChildNode(level.rootNode) I get the following error in the console:

[SCNKit ERROR] removing the root node of a scene from its scene is not allowed

I am not sure why this error is coming up, but I am trying to load my level.dae file in and add it to the scene. From what I can see in the simulator (and device), it loads fine.

What should I do to prevent the error message from occuring?

4

1 回答 1

17

根节点很特殊——它们不能从场景中脱离父节点并移动到新场景中。您需要将一个子节点或后代节点拉出您的节点levelScene才能进入您的游戏场景。例如:

let heroScene = SCNScene(named: "hero.dae")
if let heroNode = heroScene.rootNode.childNodeWithName("heroGroup", recursively: true) {
    scene.rootNode.addChildNode(heroNode)
}

或全部移动:

for node in levelScene.rootNode.childNodes as [SCNNode] {
    scene.rootNode.addChildNode(node)
}
于 2014-07-11T17:42:11.720 回答