So currently I am trying to make an app where when the player collides with the enemy, the enemy disappears. I have achieved this by writing this code;
func didBegin(_ contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.node?.name == "Player" {
firstBody = contact.bodyA
secondBody = contact.bodyB
}else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.node?.name == "Player" && secondBody.node?.name == "Enemy" {
}
if contact.bodyA.categoryBitMask == 1 && contact.bodyB.categoryBitMask == 2 {
self.enumerateChildNodes(withName: "Enemy") { (node:SKNode, nil) in
if node.position.y < 550 || node.position.y > self.size.height + 550 {
node.removeFromParent()
}
}
}
}
However, because I'm enumeratingChildNodes with the name "Enemy", every enemy disappears on screen. I only want the one I hit to disappear. Any help? Thanks!