5

Is there some way in Swift that I can tell when an SKSpriteNode has actually been removed from the scene? I don't think it's actually done when removeFromParent is called, but instead I think it's done later, when Sprite-Kit thinks it's convenient to do so.

I'm trying to understand the full life cycle and I've noticed that a sprite can still be involved in contact and collisions in didBeginContact even after that sprite has been removed.

If I print out the contents of children (i.e. the array holding all the children of the scene) I see that the sprite is removed as soon as removeFromParent called, but the sprite is still available (at least, for this execution of the SK game loop).

Edit: This question came out from an earlier question of mine concerning didBeginContact being called multiple times for one contact (Sprite-Kit registering multiple collisions for single contact) and discovering that removing the sprite during the first contact did not prevent the subsequent contact(s). (Because SK has 'queued' to contacts in advance.) so I was wondering when the sprite is actually removed.

15
  • 2
    It is not the node which stays, but rather its physics body.. Node is removed immediately, but physics body is removed only when physics simulation is done. By the way I wrote an answer which shows how to use NSNotifications and extension to post a notification when a node is removed from a parent, and to observe that notification in different classes. But now I guess you don't need that lol
    – Whirlwind
    Nov 21, 2016 at 10:46
  • 1
    @Whirlwind and I have written many answers on how to handle didBeginContact. I would not recommend using NSNotifications for such a thing unless you need a wide spread amount of classes to get this message. Instead, do not kill your node, just flag it that it is dying (I like using categoryBitMask flag 31, but you may use a separate variable), and have the parent just check the state of your node. Then on your didEndUpdate, do the actual kill so everything is killed together. Nov 21, 2016 at 12:29
  • 1
    I agree with @Knight0fDragon. Using NSNotifications should be used sparingly and they are meant for the cases when multiple objects are interested in a certain event. So you can notify them all at once, which is what I thought you are trying to achieve (issue a message when a node is removed so that other objects can be alarmed about that event).
    – Whirlwind
    Nov 21, 2016 at 12:31
  • 1
    Unless they changed something in iOS 10 this should not be the case, I will write an example later on today for you Nov 21, 2016 at 14:40
  • 1
    @Whirlwind, I believe that is what he is saying. I assumed he typoed his print statement and meant to do print("Removed /(nodeA!.name)") Nov 21, 2016 at 15:15

3 Answers 3

2
+50

Am I missing the obvious? So even after removeFromParent the sprite is still around. However, it might well be because I have assigned the node to a temporary SKSpriteNode variable, then as long as that variable is around, there is a strong reference to the node, so it won't be deallocated. Also the SKPhysicsContact object itself will retain a reference to the physicsBody, which has a reference to the node which I think will also prevent allocation.

Update

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

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

I think this can only be added in a subclass definition, not via an extension.

Having a variable with a strong reference to the node being removed will prevent the node from being de-allocated until that variable is itself removed or changed to refer to something else.

3
  • Eheheh it happen ;-). The good thing is you've solved. So, good news. Nov 30, 2016 at 15:07
  • 1
    @AlessandroOrnano Indeed. I've solved both of my problems with dBC being called multiple times for a single contact (although Knight threw me a couple of scenarios which I've not encountered myself but for which I'd have to handle dBC differently). Probably more importantly I've had to think about what's happening behind the scenes why why things work they way they do.
    – Steve Ives
    Nov 30, 2016 at 15:13
  • 1
    I think you've nailed it with this temporary variable solution. This is how I was intuitively thinking about doing this, but without words to describe it. I needed a mind much smarter than mine to express it so I could more fully conceive and consider it, and I think I'm getting it, now. The reference in the SKSpriteNode variable, although not added to a scene, exists in the SpriteKit world, and is therefore addressable, can run actions, and call out to anything else in the world. Have I got this right?
    – Confused
    Dec 3, 2016 at 2:35
1

If I've understand your question, I think it's not needed because you can always do:

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

Hope it helps you, you can write this code wherever you think is necessary.

Update:

About the reformulation of your question take a look below to these comments.

4
  • Alessandro - sorry - I've reworded my question a bit to add more context. I'm trying to get to the bottom of this "Never remove a sprite in didBeginContact" because it might cause a problem" advice, and I can't get it to cause a problem.
    – Steve Ives
    Nov 21, 2016 at 10:33
  • Steve take a look to this answer stackoverflow.com/a/38497607/1894067 , in the same thread there is also your answer and let me know if I've understand what do you want to achieve ;) Nov 21, 2016 at 10:37
  • I wasn't so much interested in where/when to remove the sprite, but where or when SK actually deallocates it.
    – Steve Ives
    Nov 30, 2016 at 14:08
  • 1
    I think you should follow this order of events :- 1) stop/remove/disable (you decide) the physics from your node (physics should not be involved so there isnt a new next collision). 2) remove your node. You shouldn't have any problem after that. Nov 30, 2016 at 14:18
1

I have a suggestion after reading through the comments... move the node to a place outside of the playable area of your game, then remove it from parent. At this point you don't have to worry about when the physics body gets removed or when SK handles it. Or you could set the physicsBody? to nil at the same time, or use a bitmask flag as KoD suggested.

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

2
  • I've checked those docs but couldn't see anything about node deallocation.
    – Steve Ives
    Nov 30, 2016 at 10:31
  • @SteveIves I meant after you override the methods, you can run a check such as Alessandro suggested to find out when / where your stuff dies automatically.
    – Fluidity
    Nov 30, 2016 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.