Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all nodes in Sprite Kit in Swift?

I have a game where a contact with the enemy ends the game but when I use removeFromParent on the enemy node it only removes the most recent one. The enemies are spawned using a switch statement to determine what side they enter from and once a contact happens and the game ends the nodes still present mess up the game by causing more contacts. If the user doesn't press anything and the enemies finish their trajectory during the game over screen then the problem doesn't occur.

Could I kill all the nodes from the screen any way once the game ends? Could I suspend input for like 3 seconds during the end screen to let the actions finish? Do I need to enumerate the enemies as they spawn to kill them off one by one once the game ends?

I've been stuck on this for like 3 days and would love some help. I think I explained it fairly well but if I need to show any code please ask and I will. I'm very new to swift by the way.

like image 322
yellowlan Avatar asked Jun 27 '16 15:06

yellowlan


2 Answers

In your scene class you can write

// For all children
self.removeAllChildren()

// Removing Specific Children
for child in self.children { 

   //Determine Details
    If child.name == "bob" {
        child.removeFromParent 
    }
}
like image 149
Peter Larson Avatar answered Oct 30 '22 00:10

Peter Larson


You can use the SKNode's method removeAllChildren(). Call it in the enemies' parent node. If it's the scene, the call should be scene.removeAllChildren().

like image 22
Ivens Denner Avatar answered Oct 30 '22 00:10

Ivens Denner