Getting a sprite to send a message when it is removed from the scene after being deleted from Parent?

Is there some way in Swift that I can tell when SKSpriteNode really removed from the scene? I don’t think it really was done when removeFromParent is removeFromParent , but instead I think it was done later when the Sprite-Kit thinks it’s convenient.

I am trying to understand the full life cycle, and I noticed that the sprite can still participate in contacts and collisions in didBeginContact even after deleting the sprite.

If I print the contents of children (i.e. an array containing all the children of the scene), I see that the sprite is deleted immediately after the call to removeFromParent , but the sprite is still available (at least for this execution of the SK game loop).

Edit: this question arose from an earlier question that didBeginContact called several times for one contact ( Sprite-Kit, registering several collisions for a single contact ) and the discovery that sprite removal during the first contact does not interfere with the subsequent contact (contacts) . (Because SK "queued" for contacts in advance.), So I was wondering when the sprite was actually deleted.

+5
source share
3 answers

Am I missing the obvious? So even after removeFromParent sprite is still around. However, this may be due to the fact that I assigned the node temporary variable SKSpriteNode , then, while this variable is around, there is a strong link to node, so it will not be released. Also, the SKPhysicsContact object SKPhysicsContact keep a reference to a physical element that references a node, which I think will also prevent selection.

Update

To see when the sprite is actually released, use the deinit() method:

 deinit { print("Invader of type \(type) deinitialised") } 

I think this can only be added to the definition of a subclass, not through an extension.

Having a variable with a strong reference to the node to be deleted will prevent node allocation until the variable itself is deleted or modified to refer to something else.

+2
source

If I understand your question, I think this is not necessary, because you can always:

 if let myNode = self.childNode(withName: "//myNode") { // ok myNode exist } 

Hope this helps you, you can write this code wherever you think.

Update:

To reformulate your question, take a look at these comments below.

+1
source

I have a suggestion after reading the comments ... move the node to a place outside the game area of ​​your game, and then remove it from the parent. At this point, you don’t have to worry about when the physical body is removed or when the SK processes it. Or can you install physicsBody? on nil at the same time or use the bitmask flag, as suggested by KoD.

You can override all the functions in the SK loop and check to see exactly when your pb is deleted: https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html

+1
source

Source: https://habr.com/ru/post/1260070/


All Articles