代码之家  ›  专栏  ›  技术社区  ›  jonpeter

如何删除最后放置的子节点?

  •  1
  • jonpeter  · 技术社区  · 7 年前

    我已经触摸开始发现,然后在用户点击屏幕时放置一个节点。

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
        if let touchLocation = touches.first?.location(in: sceneView)
        {
            let hitTestResults = sceneView.hitTest(touchLocation, types: .featurePoint)
    
            if let hitResult = hitTestResults.first
            {
                addDot(at: hitResult)
            }
        }
    }
    
    func addDot(at hitResult : ARHitTestResult)
    {
    
        let imagePlane = SCNPlane(width: 0.1, height: 0.1)
        imagePlane.firstMaterial?.diffuse.contents = UIImage(named: "helmet_R.png")
     let planeNode = SCNNode(geometry: imagePlane)
    
       planeNode.position = SCNVector3(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)
    
        planeNode.constraints = [SCNBillboardConstraint()]
    
    
        sceneView.scene.rootNode.addChildNode(planeNode)
    

    我尝试了一个简单的

    planeNode.removeFromParentNode()
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   nagam11    7 年前

    您应该创建对类中最后添加的节点的引用。

    var lastNode: SCNNode?
    

     sceneView.scene.rootNode.addChildNode(planeNode)
     lastNode?.removeFromParentNode()
     lastNode = planeNode