1
votes

So I'm currently making a game in Xcode using SpriteKit and coding in Swift. I was wondering if there was a way to remove a single child from a parent when a certain condition is met. I see the removeAllChildren() method and removeChildren(in: [SKNode]). I do not want to remove all children and I haven't found a way to make the removeChildren(in: [SKNode]) method work for me. Any help would be appreciated (maybe an explanation of how to use the aforementioned to remove a specific child?). Thanks.

David

1

1 Answers

4
votes

This should work for you (this assumes that the child is a SKSpriteNode, you could cast it as anything you want)

if let child = self.childNode(withName: "object") as? SKSpriteNode {
    child.removeFromParent()
}