I’m suffering in pain here with a project in Cocos2d. I have created a small project that isolates the core of my “missunderstanding”.
The following is a very simple code that creates two separate scenes and pretends to reuse the children of the first. I am using cocos2d v2 in a project using ARC.
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello ARC World" fontName:@"Marker Felt" fontSize:64];
CCScene *scene1 = [HelloWorldLayer scene];
CCScene *scene2 = [HelloWorldLayer2 scene];
[director_ pushScene: scene1];
// I add my label to the main layer of my scene1.
[[scene1 getChildByTag:1] addChild:label];
//I reset my own scene1 pointer to make sure only the director points to it.
//scene1 = nil;
// I replace the scene, and hope that the old scene1 will be killed by cocos
[director_ replaceScene: scene2];
// When I want to reuse my "label" object, I get the "child already added..." exception
[[scene2 getChildByTag:2] addChild:label];
Why is this wrong?
I’ve read that I should not mess with RemoveAllChildren and the alike, because replaceScene is supposed to do all the job for me..
Am I assuming something radically wrong here? Is the reuse of objects between different scenes strictly forbidden?
As others pointed out already, a node can only have one parent. Let me point you to the actual misunderstanding. Here’s your code again:
You created two objects scene1 and scene2. They will remain in scope until the current method ends. This is the key to your issue.
The scene1 object is now the active scene from cocos2d’s point of view. You’ve added the label object as child.
The scene2 object is not the active scene in cocos2d. The scene1 object however is still in scope (local scope of the current method), therefore it is not deallocated until after the current code block exits (ie the method returns).
Here you try to add label as child node to scene2 which already is a child node in scene1. BAMM! And for good reason.
You might want to try the following, assuming you’re using ARC this should work:
By adding extra code blocks the scene1 object should be deallocated when you run replaceScene. Why? Because scene1 is only in scope of the first code block, in the second code block the scene1 variable is already out of scope and ARC knows that it can then be released from memory assuming cocos2d also removes all strong references to scene1 during the replaceScene method.
This last assumption is what I’m unsure of because cocos2d itself doesn’t use ARC, so it’s possible that the scene1 object does continue to live on after replaceScene due to the way cocos2d’s autorelease works. At the latest the scene1 object will be released before the next frame begins.