Reputation: 3266
I am trying to quickly detach a node from its parent and attach to another node, in order to accurately follow the position of the parent. I use the following code:
// detach from parent
[_player removeFromParent];
// attach back to world node, at the same visual location, before taking action
[_player setCenterPosition: CGPointMake(newPosX,newPosY)];
[_worldNode addChild:_player];
However, I have flickering at the second running this code, and it does not look flawless. Is there a faster way to attach/detach node from its parent?
Upvotes: 1
Views: 1071
Reputation: 6751
Just got back to this question, didn't see your reply in comments until now.
Your clarification in the comments, should be added to the question :
"what better way there is to "attach" one node to another, assuming that using 'joints' won't help in this case?"
One concept you could use is to subclass and create a iVar or property where you store a reference to the object that you want to attach it to.
An alternative without subclassing would be to store that reference in the userData
property of the node.
Then during the update each frame, you could use that reference to update the given nodes position to it's desired parent node.
Here's an example using a flea on a dog , storing in userData
:
flea.userData = [[NSMutableDictionary alloc]init];
[flea.userData setObject:dog forKey:@"parent"];
Then in your update when dealing with the flea, you'd do this :
SKNode *fleaParent = [flea.userdata valueForKey:@"parent"];
flea.position = fleaParent.position;
Of course if flea and dog are in a different coordinate space you'd need to address that as well, but this is just the basic concept of storing a reference.
Upvotes: 0
Reputation: 11696
You cannot transfer a child from one parent to another. The only option you have is to remove the node from parent and add to another parent which, as you already figured out, creates an on/off effect.
Not much you can do about a flicker except change your game logic as to prevent having to switch parents for your node.
Upvotes: 2