Reputation: 305
Is there a property of an SKSpriteNode
which can be used to tell if it has been removed from the parent?
For example,
self.addChild(sprite)
print(sprite.isRemoved) //prints false
sprite.removeFromParent()
print(sprite.isRemoved) //prints true
Upvotes: 1
Views: 68
Reputation: 14995
You check write your function or variable like this using guard statement.
var isRemoved: Bool {
guard let parent = sprite.parent else {return true}
return false
}
And you can check this isRemoved variable in your code whether it has been removed from the parent or not.
Upvotes: 0
Reputation: 10920
All SKNode's have a parent property which is an optional. So you can see if the node has a parent.
if sprite.parent == nil {
}
Upvotes: 3
Reputation: 58029
You can check whether the parent
attribute of the SKNode
is nil
.
if sprite.parent == nil {
print("sprite has been removed from the parent")
}
Upvotes: 0