你如何在dae场景之间来回转换?

时间:2015-02-21 17:32:15

标签: ios swift scenekit

这是一个稍微修改过的启动器SceneKit项目 - 我添加了另一个名为planet.dae的.dae文件。点击“发货”切换到planet.dae。点击行星切换回ship.dae。攻击船确实打开了planet.dae。但当用户点击这个星球时,没有任何反应 - 似乎没有认出我的触摸。是什么给了什么?

 import SceneKit
 class GameViewController: UIViewController {

 override func viewDidLoad() { super.viewDidLoad()
    shipScene()
}

func shipScene() {
    let scene = SCNScene(named: "art.scnassets/ship.dae")!
    let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!
    let scnView = self.view as SCNView
    scnView.scene = scene
    scnView.autoenablesDefaultLighting = true
    scnView.allowsCameraControl = true
    scnView.backgroundColor = UIColor.blackColor()

    let tapGesture = UITapGestureRecognizer(target: self, action: "handleShipTap:")
    let gestureRecognizers = NSMutableArray()
    gestureRecognizers.addObject(tapGesture)
    if let existingGestureRecognizers = scnView.gestureRecognizers {
        gestureRecognizers.addObjectsFromArray(existingGestureRecognizers)
    }
    scnView.gestureRecognizers = gestureRecognizers
}

func handleShipTap(gestureRecognize: UIGestureRecognizer) {
    let scnView = self.view as SCNView
    let p = gestureRecognize.locationInView(scnView)
    if let hitResults = scnView.hitTest(p, options: nil) {
        if hitResults.count > 0 {
            let result: AnyObject! = hitResults[0]

            if result.node!.name!.hasPrefix("ship") {
                planetScene()
            }
        }
    }
}
func planetScene() {
    let scene = SCNScene(named: "art.scnassets/saturn.dae")!
    let ship = scene.rootNode.childNodeWithName("planet", recursively: true)!
    let scnView = self.view as SCNView
    scnView.scene = scene
    scnView.autoenablesDefaultLighting = true
    scnView.allowsCameraControl = true
    scnView.backgroundColor = UIColor.blackColor()

    let tapGesture = UITapGestureRecognizer(target: self, action: "handlePlanetTap:")
    let gestureRecognizers = NSMutableArray()
    gestureRecognizers.addObject(tapGesture)
    if let existingGestureRecognizers = scnView.gestureRecognizers {
        gestureRecognizers.addObjectsFromArray(existingGestureRecognizers)
    }
    scnView.gestureRecognizers = gestureRecognizers
}

func handlePlanetTap(gestureRecognize: UIGestureRecognizer) {
    let scnView = self.view as SCNView
    let p = gestureRecognize.locationInView(scnView)
    if let hitResults = scnView.hitTest(p, options: nil) {
        if hitResults.count > 0 {
            let result: AnyObject! = hitResults[0]

            if result.node!.name!.hasPrefix("saturn") {
                shipScene()
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

从行星场景()中移除水龙头手势。