Checking if Nodes are SKShapeNodes

104 views Asked by At

In my simple game, I need to remove all SKShapeNodes before the next step happens. Here is the code that I have:

for child in self.children(){
    if(child==SKShapeNode(){
        child.removeFromParent()
    }
}

This does not work because Xcode does not know what to do with child's class. Xcode wants me to add

as! NSObject

after child to "force downcast". This also does not work. I think that this is because now, child will always be an NSObject, not an SKShapeNode.

How do I fix this? I am sure that it is very simple but I cannot seem to fix this myself.

1

There are 1 answers

0
Unheilig On BEST ANSWER

You could do it as follows:

for child in parent.children //in you case, self is the parent
{
    if let child = child as? SKShapeNode
    {
        child.removeFromParent()
    }
}