In my game I have two SKNodes called "Center" rotating.
I have several objects as the child of Center (the SKNode). I did this so I could let these objects orbit around Center. Although at certain points I want to switch to other SKNodes - "Center2". In this I have named one of the objects "Onject", which switched between parents I need to attach and detach at certain points in my game. I have tried, after doing self.addChild(Object) then doing Center.addChild(Object). My game crashed.
How do I switch between parents for objects?
there is a method called move(toParent:SKNode) to do exactly what you want, it will also fix the coordinates for you.
If you call self.addChild(Object) and then Center.addChild(Object) your game will crash, because the object will then have two parents. You cant add the object twice as a child for different nodes.
If you want to switch the parent, you have to remove it first from its parent, so the flow will be as follows:
self.addChild(Object) (then just perform your game logic)once its time to switch the parent, follow these steps:
Object.removeFromParent()Center.addChild(Object)Then your object has a new parent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With