Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:47:59+00:00 2026-06-17T11:47:59+00:00

I’m suffering in pain here with a project in Cocos2d. I have created a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:48:00+00:00Added an answer on June 17, 2026 at 11:48 am

    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:

    CCScene *scene1 =  [HelloWorldLayer scene];
    CCScene *scene2 =  [HelloWorldLayer2 scene];
    

    You created two objects scene1 and scene2. They will remain in scope until the current method ends. This is the key to your issue.

    [director_ pushScene: scene1];
    [[scene1 getChildByTag:1] addChild:label];
    

    The scene1 object is now the active scene from cocos2d’s point of view. You’ve added the label object as child.

    [director_ replaceScene: scene2];
    

    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).

    [[scene2 getChildByTag:2] addChild:label];
    

    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:

    {
        CCScene *scene1 =  [HelloWorldLayer scene];
        [director_ pushScene: scene1];
        [[scene1 getChildByTag:1] addChild:label];
    }
    {
        CCScene *scene2 =  [HelloWorldLayer2 scene];
        [director_ replaceScene: scene2];
        [[scene2 getChildByTag:2] addChild:label];
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.