Registering touch controls to remove game objects
Next, let's add the functionality to remove the anchor node (that is, at the crosshair) and decrease the score.
In the touchesBegan
function, remove all the current code and add the following:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let location = crosshair.position let hitNodes = nodes(at: location) for node in hitNodes { if(node.name == "enemy"){ node.removeFromParent() score-=1 self.scoreText.text = "\(score)" break } } }
Here, we set the location to the crosshair to the center of the screen. We get all the nodes that are present at that location. Then, we cycle through all the nodes in hitNodes
. If the name of the node is enemy, then we remove the node from the parent, decrease...