错误:不允许从场景中删除场景的根节点

时间:2017-08-14 20:06:57

标签: ios swift runtime-error scenekit

我在代码的最后一行收到错误。

- (SCNScene *)getWorkingScene {
    SCNScene *workingSceneView  = self.scene;

    if (workingSceneView == nil){
        workingSceneView = [[SCNScene alloc] init];
        workingSceneView.background.contents = self.skyColor;
        self.scene = workingSceneView;
        self.allowsCameraControl = TRUE;
        self.autoenablesDefaultLighting = TRUE;
        self.showsStatistics = TRUE;
        self.backgroundColor = self.skyColor;
        self.delegate = self;
    }

    return workingSceneView;
}

DPoint *point = [coodinate convertCooridnateTo3DPoint];
NSURL *pathToResource = [NSURL urlWithObjectName:objectName ofType:@"dae"];
NSError *error;
SCNScene *scene = [SCNScene sceneWithURL:pathToResource options:nil error:&error];
SCNNode *node = scene.rootNode;
node.position = SCNVector3Make(point.x, point.y, point.z);
node.rotation = SCNVector4Make(0, 1, 0, ((M_PI*point.y)/180.0));
SCNScene *workingScene = self.getWorkingScene;
[workingScene.rootNode addChildNode:node];

1 个答案:

答案 0 :(得分:2)

一个节点只能属于一个场景,就像一个视图只能有一个父视图一样。

当您致电[workingScene.rootNode addChildNode:node];时,您正在将node从当前场景(scene)移动到另一个场景(workingScene)。但nodescene的根节点。您不能删除场景的根节点,因此会出错。

一种解决方案是将node的所有子节点移动到workingScene.rootNode