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 8430335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:34:37+00:00 2026-06-10T05:34:37+00:00

In the cocos2d-x HelloWorld project, I am trying to add another layer to the

  • 0

In the cocos2d-x HelloWorld project, I am trying to add another layer to the scene and keep a reference to this layer in a data member. Since the function HelloWorld::scene() is static, I cannot add the layer within this function (because I cannot set the data member for the layer).

So I tried getting the scene in the init() function as follows, however this leads to scene = 0x00000000.

What am I doing wrong?

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());

        CCScene* scene = NULL;
        scene = CCDirector::sharedDirector()->getRunningScene();

        // add another layer
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
        // set data member
        this->layerHud = layerHud;

        // next line crashes (because scene  is 0x00000000)
        scene->addChild(layerHud);

    bRet = true;
    } while (0);
    return bRet;
}

PS: The reason that I want to add the hud layer to the scene, rather than to the current layer, is because I am moving the current layer around and do not want the hud layer to be moved with it.


Edit: Since the accepted answer allowed for multiple options, here’s what I did to fix the problem:

1.) Removed the HUD layer from the init() function:

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());

    bRet = true;
    } while (0);
    return bRet;
}

2.) And instead added the HUD layer to the scene function (which also is the way its done in cocos2d-iphone):

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // scene
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // HelloWorld layer
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);
        scene->addChild(layer);

        // HUD layer
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
        scene->addChild(layerHud);

        // set data member
        layer->layerHud = layerHud;

    } while (0);

    // return the scene
    return scene;
}

Essentially the problem was that my assumption, “Since the function HelloWorld::scene() is static, I cannot add the layer within this function (because I cannot set the data member for the layer).”, was wrong.

  • 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-10T05:34:38+00:00Added an answer on June 10, 2026 at 5:34 am

    The scene is NULL because you call getRunningScene() even before the first scene is constructed.

    And one of your assumption is wrong. HelloWorld::scene() is static, but you still can add the layer within this function (and you can set the data member for the layer)

    the correct way is to create a new public function

    setLeyerHud(HelloWorldHud* hud);
    

    in HelloWorld Class and in the CCScene* HelloWorld::scene() function, add the following lines:

    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
    
        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);
    
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
    
        scene->addChild(layer);
    
        scene->addChild(layerHud);
    
        layer->setLayerHud(layerHud);
    
    } while (0);
    

    it depends on weather you want to add the Hud into the HelloWorld Layer or you want to add to the scene.

    I. add new layer to the scene:

    remove code

        CCScene* scene = NULL;
        scene = CCDirector::sharedDirector()->getRunningScene();
    
        // add another layer
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
        // set data member
        this->layerHud = layerHud;
    
        // next line crashes (because scene  is 0x00000000)
        scene->addChild(layerHud);
    

    from

    HelloWorld::init()
    

    and change code in

    CCScene* HelloWorld::scene()
    

    to

    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
    
        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);
    
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
    
        scene->addChild(layer);
    
        scene->addChild(layerHud);
    } while (0);
    
    // return the scene
    return scene;
    

    II. add new layer to the HelloWorld Layer:

    change the code

        CCScene* scene = NULL;
        scene = CCDirector::sharedDirector()->getRunningScene();
    
        // add another layer
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
        // set data member
        this->layerHud = layerHud;
    
        // next line crashes (because scene  is 0x00000000)
        scene->addChild(layerHud);
    

    in

    HelloWorld::init()
    

    to

        // add another layer
        HelloWorldHud* layerHud = HelloWorldHud::create();
        CC_BREAK_IF(! layerHud);
    
        // next line crashes (because scene  is 0x00000000)
        this->addChild(layerHud);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Cocos2d-x and trying to detect touches in my HelloWorld project. Though I'm
I use the following code to add cocos2d scene to a viewcontroller - (void)viewDidLoad
I am trying to place a series of sprites on a layer, but since
When i compile my cocos2d mac project i get this error: Undefined symbols for
I'm newbie. Trying to run HelloWorld example comming with cocos2dx (cocos2d-2.0-rc2-x-2.0.1), Android SDK 20.0.1,
I have a helloworld scene that I would like to add a subview to.
Using cocos2d for iPhone game development, I am confused between Layer and Scene. For
Consider this: you create a new project on Cocos2D 2.0. You have the traditional
In Cocos2d, I have read that [[[[CCDirector sharedDirector] openGLView] window] addSubview:someButton]; will add a
I'm trying to write a singleton class for maintain game data, It's called GameManager,

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.