mahclark
mahclark

Reputation: 305

Swift - Boolean property of SKSpritenode - is removed from parent?

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

Answers (3)

Hussain Shabbir
Hussain Shabbir

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

Kyle Goslan
Kyle Goslan

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

user3151675
user3151675

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

Related Questions