title: Adding another layer to the scene in the HelloWorld project
body: <p>In the cocos2d-x HelloWorld project, I am trying to add another layer to the <em>scene</em> and keep a reference to this layer in a data member. Since the function <code>HelloWorld::scene()</code> is static, I cannot add the layer within this function (because I cannot set the data member for the layer).</p>
  
  <p>So I tried getting the scene in the <code>init()</code> function as follows, however this leads to <code>scene = 0x00000000</code>.</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>bool HelloWorld::init()
  {
      bool bRet = false;
      do 
      {
          CC_BREAK_IF(! CCLayer::init());
  
          CCScene* scene = NULL;
          scene = CCDirector::sharedDirector()-&gt;getRunningScene();
  
          // add another layer
          HelloWorldHud* layerHud = HelloWorldHud::create();
          CC_BREAK_IF(! layerHud);
          // set data member
          this-&gt;layerHud = layerHud;
  
          // next line crashes (because scene  is 0x00000000)
          scene-&gt;addChild(layerHud);
  
      bRet = true;
      } while (0);
      return bRet;
  }
  </code></pre>
  
  <p>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.</p>
  
  <hr>
  
  <p>Edit: Since  the accepted answer allowed for multiple options, here's what I did to fix the problem:</p>
  
  <p>1.) Removed the HUD layer from the init() function:</p>
  
  <pre><code>bool HelloWorld::init()
  {
      bool bRet = false;
      do 
      {
          CC_BREAK_IF(! CCLayer::init());
  
      bRet = true;
      } while (0);
      return bRet;
  }
  </code></pre>
  
  <p>2.) And instead added the HUD layer to the scene function (which also is the way its done in cocos2d-iphone):</p>
  
  <pre><code>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-&gt;addChild(layer);
  
          // HUD layer
          HelloWorldHud* layerHud = HelloWorldHud::create();
          CC_BREAK_IF(! layerHud);
          scene-&gt;addChild(layerHud);
  
          // set data member
          layer-&gt;layerHud = layerHud;
  
      } while (0);
  
      // return the scene
      return scene;
  }
  </code></pre>
  
  <p>Essentially the problem was that my assumption, "Since the function <code>HelloWorld::scene()</code> is static, I cannot add the layer within this function (because I cannot set the data member for the layer).", was wrong.</p>