Group Group Group Group Group Group Group Group Group

Continuously spawn and delete nodes

#1

I want to spawn a node every 5 seconds and then delete the node when it reaches a certain position.
I have created code to this but I get a stutter effect.

I spawn a new node

    let spawnNode = SCNNode();
    
    let spawnAction = SCNAction.run({ node in
        self.testEnemy();
    })
   
    let delayAction = SCNAction.wait(duration: 5, withRange: 5);
    
    spawnNode.runAction(SCNAction.repeatForever(SCNAction.sequence([delayAction, spawnAction])));
    
    self.rootNode.addChildNode(spawnNode);

The node that is created and added to the scene

    let carNode = carScene!.rootNode.childNode(withName: "enemy1", recursively: false)!.clone() as SCNNode
    
    carNode.name = "Car"
    carNode.scale = SCNVector3(0.25, 0.25, 0.25);
    carNode.position = SCNVector3(x: 25, y: (self._Gplatform?.position.y)! + 0.5, z: self.ArrayOfGroundPositions())
    
    self.rootNode.addChildNode(carNode)
    
    let moveAction = SCNAction.move(by: SCNVector3(x: -50.0, y: 0.0, z: 0.0), duration: 5.0)
    let removeAction = SCNAction.run { node -> Void in
        node.removeFromParentNode()
    }
    carNode.runAction(SCNAction.sequence([moveAction, removeAction]))

If anyone can guide me in the right direction It would be very helpful.