SCNSceneRenderer的Swift问题---无法让协议工作

时间:2016-12-21 12:42:14

标签: swift swift-protocols

我正在努力实施Zig Zag游戏并遵循在线课程的一系列说明。我有一个快速的UIViewController,我现在正在尝试使用SCNSceneRenderer(用于保持游戏角色在Z字形路径上的游戏逻辑)。代码如下所示:

import UIKit
import QuartzCore
import SceneKit

class GameViewController: UIViewController, SCNSceneRenderer{

    let scene = SCNScene()
    let cameraNode = SCNNode()

    var person = SCNNode()

    let firstBox = SCNNode()

    var goingLeft = Bool()

    var tempBox = SCNNode()
    var boxNumber = Int()
    var prevBoxNumber = Int()

    override func viewDidLoad() {
        print("Yes -- view did load")
        self.createScene()
    }

    func renderer(render: SCNSceneRenderer, updateAtTime time: TimeInterval){

        let deleteBox = self.scene.rootNode.childNode(withName: "\(prevBoxNumber)", recursively: true )

        if (deleteBox?.position.x)! > (person.position.x + 1) || (deleteBox?.position.z)! > (person.position.z + 1) {
            prevBoxNumber += 1
            deleteBox?.removeFromParentNode()

            createBox()
        }   
    }

    func createBox(){
        tempBox = SCNNode(geometry: firstBox.geometry)
        let prevBox = scene.rootNode.childNode(withName: "\(boxNumber)", recursively: true )

        boxNumber += 1
        tempBox.name = "\(boxNumber)"

        let randomNumber = arc4random() % 2
        switch randomNumber {
        case 0:
            tempBox.position = SCNVector3Make((prevBox?.position.x)! - firstBox.scale.x, (prevBox?.position.y)!, (prevBox?.position.z)!)
           break

        case 1:
            tempBox.position = SCNVector3Make((prevBox?.position.x)! , (prevBox?.position.y)!, (prevBox?.position.z)! - firstBox.scale.z)
            break

        default:
           break
        }

        self.scene.rootNode.addChildNode(tempBox)

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if goingLeft == false  {
            person.removeAllActions()
            person.runAction(SCNAction.repeatForever(SCNAction.move(by: SCNVector3Make(-100, 0, 0), duration: 20)))
            goingLeft = true

        } else {
            person.removeAllActions()
            person.runAction(SCNAction.repeatForever(SCNAction.move(by: SCNVector3Make(0, 0, -100), duration: 20)))
            goingLeft = false    
    }

        print("boxNumber is \(boxNumber)")
    }




    func createScene(){
        boxNumber = 0
        prevBoxNumber = 0

        self.view.backgroundColor = UIColor.orange
        let sceneView = self.view as! SCNView
        sceneView.delegate = self
        sceneView.scene = scene

        //Create Person
        let personGeo = SCNSphere(radius: 0.2)
        person = SCNNode (geometry : personGeo)
        let personMat = SCNMaterial()
        personMat.diffuse.contents = UIColor.red
        personGeo.materials = [personMat]
        person.position = SCNVector3Make(0, 1.1, 0)
        scene.rootNode.addChildNode(person)


        //Create Camera
        cameraNode.camera = SCNCamera()
        cameraNode.camera?.usesOrthographicProjection = true
        cameraNode.camera?.orthographicScale = 3
        cameraNode.position = SCNVector3Make(20,20,20)
        cameraNode.eulerAngles = SCNVector3Make(-45,45,0)
        //let constraint = SCNLookAtConstraint(target: firstBox)
        let constraint = SCNLookAtConstraint(target: person)
        constraint.isGimbalLockEnabled = true
        self.cameraNode.constraints = [constraint]
        scene.rootNode.addChildNode(cameraNode)
        person.addChildNode(cameraNode)


        //Create Box
        let firstBoxGeo = SCNBox(width: 1.0, height: 1.5, length: 1.0, chamferRadius: 0)
        firstBox.geometry = firstBoxGeo
        let boxMaterial = SCNMaterial()
        boxMaterial.diffuse.contents = UIColor(red: 0.2, green: 0.8, blue: 0.9, alpha: 1.0)
        firstBoxGeo.materials = [boxMaterial]
        firstBox.position = SCNVector3Make(0,0,0)
        scene.rootNode.addChildNode(firstBox)
        firstBox.name = "\(boxNumber)"


        for _ in 0...6{
            createBox()
        }

        //Create light
        let light = SCNNode()
        light.light = SCNLight()
        light.light?.type = SCNLight.LightType.directional
        light.eulerAngles = SCNVector3Make(-45, 45, 0)
        scene.rootNode.addChildNode(light)

        //Create light2
        let light2 = SCNNode()
        light2.light = SCNLight()
        light2.light?.type = SCNLight.LightType.directional
        light2.eulerAngles = SCNVector3Make(45, 45, 0)
        scene.rootNode.addChildNode(light2)  

    }

}

当我添加SCNSceneRenderer时,我收到以下错误:

&#34;输入&#39; GameViewController&#39;不符合协议&#39; SCNSceneRenderer&#39;因为它有不能满足的要求&#34;

由于我的GameViewController未被识别为SCNSceneRenderer,我在这一行也收到错误:

sceneView.delegate = self 

错误是&#34;无法分配类型&#39; GameViewController&#39;输入&#39; SCNSceneRendererDelegate&#39;

我是swift编程的新手,但这似乎是我尝试实现像Java这样的界面,我一直在查看Swift文档但是看不到我需要的东西让我的类作为SCNSceneRenderer运行。我很感激帮助解决这个问题。谢谢!

0 个答案:

没有答案