Swift Playgrounds - SCNox上的SCNHitTestResults失败

时间:2017-03-30 05:54:34

标签: scenekit swift-playground

所以我遇到了这个问题,我正在尝试制作一个SCNBox并检测它被点击的哪一侧。创建框可以正常工作 - 然而,在试图找出哪一边被挖掘时,我遇到了困难。

我的计划是像这样使用SCNHitTestResult ......

class HomeViewController: UIViewController {
var sceneView: SCNView!

override func viewDidLoad() {

}
override func viewDidAppear(_ animated: Bool) {
      //Setting up scene view and box - no problems here
    sceneView?.removeFromSuperview()

    self.view.backgroundColor = UIColor.white

    sceneView = SCNView(frame: CGRect(x: 0, y: 0, width:    self.view.bounds.width, height: self.view.bounds.height))
    self.view.addSubview(sceneView)

    let scene = SCNScene()


    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3Make(0, 0, 25)
    scene.rootNode.addChildNode(cameraNode)

    let boxGeometry = SCNBox(width: 7.5, height: 7.5, length: 7.5,   chamferRadius: 0.75)
    let boxNode = SCNNode(geometry: boxGeometry)
    scene.rootNode.addChildNode(boxNode)

    sceneView.scene = scene

    sceneView.autoenablesDefaultLighting = true
    sceneView.allowsCameraControl = true

      //Tap Gesture for when the view is tapped
    let tapGesture = UITapGestureRecognizer(target: self, action:   #selector(self.tappedFace(_:)))
     self.sceneView!.addGestureRecognizer(tapGesture)

 //Make these scene materials that can correspond with their respective   side...
    let top = SCNMaterial()
    let bottom = SCNMaterial()
    let left = SCNMaterial()
    let right = SCNMaterial()
    let front = SCNMaterial()
    let back = SCNMaterial()
    boxGeometry.materials = [ front, right, back, left, top, bottom ]
}

func tappedFace(_ sender: UITapGestureRecognizer) {
    print("Scene was pressed somewhere")

      //Get the location of where the user pressed
    var p = sender.location(in: sceneView)
      //Get the hit test results - this is where the playground crashed
    var hitResults = sceneView.hitTest(p, options: nil)
      //Everything else checks out..
    if let result = hitResults.first {
        let node = result.node
        let material = node.geometry!.materials[result.geometryIndex]
        enum CubeFace: Int {
            case Front, Right, Back, Left, Top, Bottom
        }
        print("hit face: \(String(describing: CubeFace(rawValue:   result.geometryIndex)))")
    }
}
}

每当我实际点击节点或侧面时,操场崩溃并给我以下错误:

(无法启动进程。无法附加到游戏执行的存根:错误:找不到进程ID为29871的进程)

每当我将此代码作为完整的iOS应用程序的一部分运行时,它就会成功并且完美运行。 这有什么已知的解决方法吗?或者可能有其他方法来检测盒子的哪一侧被轻敲?

0 个答案:

没有答案