2016-03-12 96 views
1

我想在Swift中構建一個SceneKit-App。這個應用程序應該有一個SpriteKit覆蓋。我的問題是,我可以從我的ViewController中的SpriteKit訪問LabelNode,但無法更改SceneKit中Cube-Node的位置值,儘管我可以訪問該節點。因爲我是一個新手,這將是非常好的,如果任何人都可以指向正確的方向。從ViewController中訪問SceneKit-Node

class GameViewController: UIViewController { 

    var sceneView: SCNView! 
    var spriteScene: SpriteKitOverlay! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)) 
     self.sceneView.scene = SceneKitScene() 
     self.sceneView.backgroundColor = UIColor.brownColor() 
     self.view.addSubview(self.sceneView) 

     self.spriteScene = SpriteKitOverlay(size: self.view.bounds.size) 
     self.sceneView.overlaySKScene = self.spriteScene 

     SceneKitScene().cubeNode.position = SCNVector3(0,10,0) 
     spriteScene.labelTest.text = "Changed label" 
     print(SceneKitScene().cubeNode.position) 
    } 
} 

class SpriteKitOverlay: SKScene { 

    var labelTest: SKLabelNode! 

    override init(size: CGSize) { 
     super.init(size: size) 

     self.labelTest = SKLabelNode(text: "Label text") 
     self.labelTest.fontColor = UIColor.whiteColor() 
     self.labelTest.fontSize = 24 
     self.labelTest.position = CGPoint(x: size.width/2, y: size.height/2) 

     self.addChild(self.labelTest) 

    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

} 


class SceneKitScene: SCNScene { 

    var cubeNode: SCNNode! 
    var cameraNode: SCNNode! 
    var lightNode: SCNNode! 

    override init() { 
     super.init() 

     let cube = SCNBox(width: 3, height: 3, length: 3, chamferRadius: 0) 
     let cubeMaterial = SCNMaterial() 
     cubeMaterial.diffuse.contents = UIColor.blueColor() 
     cube.materials = [cubeMaterial] 
     self.cubeNode = SCNNode(geometry: cube) 
     self.cubeNode.position = SCNVector3(0,0,0) 
     self.cubeNode.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 0.01, z: 0, duration: 1.0/60.0))) 

     let camera = SCNCamera() 
     camera.xFov = 60 
     camera.yFov = 60 

     let ambientLight = SCNLight() 
     ambientLight.type = SCNLightTypeAmbient 
     ambientLight.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0) 

     let cameraConstraint = SCNLookAtConstraint(target: self.cubeNode) 
     cameraConstraint.gimbalLockEnabled = true 

     self.cameraNode = SCNNode() 
     self.cameraNode.camera = camera 
     self.cameraNode.constraints = [cameraConstraint] 
     self.cameraNode.light = ambientLight 
     self.cameraNode.position = SCNVector3(x: 5, y: 5, z: 5) 

     let omniLight = SCNLight() 
     omniLight.type = SCNLightTypeOmni 

     self.lightNode = SCNNode() 
     self.lightNode.light = omniLight 
     self.lightNode.position = SCNVector3(x: -3, y: 5, z: 3) 

     self.rootNode.addChildNode(self.cubeNode) 
     self.rootNode.addChildNode(self.cameraNode) 
     self.rootNode.addChildNode(self.lightNode) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 


} 
+0

在相關說明上看rickster的回答是否應該繼承'SCNScene':http://stackoverflow.com/a/34536248/2997825 – mnuages

回答

1

當你調用SceneKitScene().cubeNode.position = SCNVector3(0,10,0),你正在試圖改變在新創建的SceneKitScenecubeNode.position

試着改變你的GameViewController到:

class GameViewController: UIViewController { 

    var sceneView: SCNView! 
    // create new instance variable to hold current sceneKitScene 
    var sceneKitScene: SceneKitScene! 
    var spriteScene: SpriteKitOverlay! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)) 
     // save SceneKitScene to instance variable 
     self.sceneKitScene = SceneKitScene() 
     self.sceneView.scene = self.sceneKitScene 
     self.sceneView.backgroundColor = UIColor.brownColor() 
     self.view.addSubview(self.sceneView) 

     self.spriteScene = SpriteKitOverlay(size: self.view.bounds.size) 
     self.sceneView.overlaySKScene = self.spriteScene 

     // update cubeNode position in active sceneKitScene 
     sceneKitScene.cubeNode.position = SCNVector3(0,10,0) 
     spriteScene.labelTest.text = "Changed label" 
     print(SceneKitScene().cubeNode.position) 
    } 
} 

這將創建一個新的實例變量來保存當前的SceneKitScene(如你做同樣的你的當前SpriteKitOverlay)。然後,您可以使用此變量訪問並更新屏幕上當前的cubeNode的位置。

+0

非常感謝Dion。這對我非常有幫助。 – panjabi

相關問題