0
votes

I used Apple's ARKit Scanner to scan an item so I can place objects around it for instructions to setup something. I used the renderer function to make that as my node to anchor objects around this. I want to create instructions for the user so when the user clicks on a button for "next step" a new node using a .scn file pops up. I called the ARKit Scanned object as baseNode and the .scn objects I am adding them to "objectNodes".

The problem is when the user presses either button for previous or next, I am adding another child to the objectNodes which I don't want. I want to be able to delete a child node from objectNodes. My question is if how can I delete the child nodes from an SCNNode.

I have tried using let temp = objectNodes.childNods, temp.removeAll() but doesn't allow me to reassign objectNodes to temp because temp becomes an array from using .childNodes. I want to be able to use the same node but just overwrite it.

@objc func nextStep() {
        switch numberOfStep {
        case 0:
            nextStep.text = "Next Step"
            previousStep.text = ""
            addBattery()
            numberOfStep += 1
        case 1:
            nodeObjects.removeFromParentNode()
            addLANObject()
}

func addBattery () {
        let battery = insertBattery(objectName: "art.scnassets/object/screwdriver.scn")
        let battery2 = insertSecondBattery(objectName: "art.scnassets/object/screwdriver.scn")
        nodeBatteryAnimation(scene: battery)
        nodeBatteryAnimation(scene: battery2)
        objectsNodes.addChildNode(battery2)
        objectsNodes.addChildNode(battery)
        baseNode.addChildNode(nodeObjects)
}

func addLANObject () {
        let etherCord = ethernet1(objectName: "art.scnassets/object/box.scn")
        nodeAnimation(scene: etherCord)
        objectsNodes.addChildNode(etherCord)
        baseNode.addChildNode(nodeObjects)
}

I added some print statements and I see that I am adding more child nodes to objectNodes. When I use objectNodes.removeFromParent(), it removes it from baseNode but when I go to the next step, I add a child node to objectNodes and re-add it to baseNode. I just want to have zero child nodes again on objectNodes .

1

1 Answers

2
votes

If you want to remove all children.

Node.children.map{$0.removeFromParentNode()}

Otherwise,filter to get the childNode first.