What exactly the HelloWorld::node() return ??

What exactly the HelloWorld::node() return ??
0.0 0



HelloWorld layer = HelloWorld::node;
I know that this static node function created by — LAYER_NODE_FUNC;
But What exactly the HelloWorld::node return ?? CCLayer or HelloWorld Class reference.
If it return layer then why we not use —> CCLayer
layer = HelloWorld::node();

I just got confuse, Plz clearify it and where I got this LAYER_NODE_FUNC macro explanation.

I add one more question if it is layer then I wanna FadeIn or FadeOut this layer, I am unable to access this layer in any other function in the same class.



The macro generates a static factory method for the particular layer, here is what the macro produces in your case:

static HelloWorld* node()
HelloWorld pRet = new HelloWorld;
if )
return pRet;
delete pRet;
pRet = NULL;
return NULL;
As you can see it instantiates a new instance of HelloWorld then it initialises it
and only if both of those are successful it returns a pointer to the new instance.
It uses your class and not CCLayer because if you don’t then you can’t access any of the HelloWorld specific items
through the returned pointer without casting it to HelloWorld*~~ on the same note returning a HelloWorld still lets
you assign it to CCLayer which is a base class of HelloWorld, i.e the following will work:
newLayer = HelloWorld::node ();

James Mintram


Thanks james mintram,

Such a good and clean explanation.

One thing is that I do this CCLayernewLayer = HelloWorld::node ();* and I want to use this layer in any other function, then i was unable to access this newLayer in another function. this is correct so i have to defined it global

So I did one thing— I defined CCLayer* newLayer in .h file then i did *newLayer = HelloWorld::node ; then it give error invalid use of member in static member fucntion, and ie. also correct in static function we use only static variable, if i defined it static then it give some terrific error.

Actually I want this layer global and use in another function……How I did This?
How and where I saw LAYER_NODE_FUNC macro definition?


In helloWorld.h you can see “LAYER_NODE_FUNC(HelloWorld)”
You can find it definition in CCLayer.h,like this

#define LAYER_NODE_FUNC(layer) \
static layer* node() \
{ \
    layer *pRet = new layer(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
  • That static void HelloWorld::scene() win return you an instance of class HelloWorld. And this method also auto call HelloWorld::init() with that instance.
    So you can defined CCLayer* newLayer in HelloWorld.h, and call newLayer = CCLayer::node() in HelloWorld::init().
    Because HelloWorld::init() isn’t static methor, it call with your instance of calss HelloWorld


I Exactly did the same thing…Defined CCLayer* newLayer in .h file then in .cpp… newLayer = HelloWorld::node (); then it give error invalid use of member in static member fucntion. Now what I supposed to do???


I’ll throw my 2 cents in, but if you’re using globals you’re probably doing it wrong. If I’m to understand correctly, in the init of a scene or class, you instantiate hello world, and then want to be able to reference the layer in a different function of the class which instantiated it? If this is the case, create CCLayer* newLayer; as a private, or protected variable of the class. Then, in your init when you instantiate HelloWorld you’ll simply do newLayer = HelloWorld::node(); Now, this is a class variable, not a global and much more clean. If you want to reference newLayer in a separate class function you can just refer to it as newLayer and it’ll be there, pointing to the object you created in init.


Look at HelloWorld::scene()

CCScene* HelloWorld::scene()
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::node();

    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::node();

    // add layer as a child to scene

    // return the scene
    return scene;

So when you call newScene = HelloWorld::scene(), it return you a new scene,which already have a new HelloWorld layer as child.
But you can use that layer directly. If you want creat a new layer and use it,you can do that:

  • in helloworld.h:

      CCLayer* newLayer;
  • in helloworld.cpp
    You put this code in HelloWorld::init(),this methor auto call by methor: HelloWorld **layer = HelloWorld::node;
    newLayer = CCLayer::node;
    /*I don’t understand why you want to creat new layer like:
    newLayer = HelloWorld::node
    you have param “this” is a helloworld layer, you can use it
    //“this” is Helloworld layer you creat with HelloWorld::node
    ~~ because helloworld is a class inherit from CCLayer, Helloworld::node() win return you a new Layer,
    and that macro: LAYER_NODE_FUNC(HelloWorld) win call HelloWorld::init() automatic

  • remember, HelloWorld::scene() return you a Scene,with a child is HelloWorld layer.
    But with every methor like HelloWorld::….(), param “this” give you exactly helloworld layer,not the scene
    So you can do every thing you want with that layer by you params “this”.
    If you want creat normal layer and use it,do as i say. You don’t need to creat a new helloworld layer
    Example, if you want to add a sprite to this layer:
    //add directly to helloworld layer
    //add to new layer you create
is that OK?


Well Explain by Rubby Entertainment and William Thurston.

Actually my problem solved but I want to clearify it…So I done what you guy said was also not working…Just what I did
in .h File
CCLayer* newLayer
in .cpp File

CCScene* HelloWorld::scene()
// ‘scene’ is an autorelease object
CCScene *scene = CCScene::node();

// ‘layer’ is an autorelease object
newLayer = HelloWorld::node();

// add layer as a child to scene

// return the scene
return scene;

Now I want to use newLayer throug out the class how i do this, Simply that is my problem….but I understand that param “this” exactly belong to HelloWorld class but I just want to clearify my problem.
Thanks for all your efforts.

  • sorry, but your code is wrong. You create newLayer, but addChild(layer)???

  • You must understand, the different when you call
    newLayer = CCLayer::node()
    and newLayer = HelloWorld::node()
    is newLayer is a HelloWorld class,which inherit from CCLayer.
    and you call newLayer = HelloWorld::node() can be understand as you call
    newLayer = CCLayer::node() and newLayer::init() ( which init() is methor that define by HelloWorld::init() )

  • If you want to do anything with instance of HelloWorld,don’t need to define newLayer in HelloWorld.h, just use “this”
    in every HelloWorld::… methor

  • If you want creat and use another layer,do what I say,
    Ok,in HelloWorld.h you defined:

    CCLayer* newLayer

you must creat it in HelloWorld::init(),not in HelloWorld::scene. HelloWorld::init() isn’t a static methor,
so you can creat and use anything you want. And don’t need to call HelloWorld::init(),it auto call by HelloWorld::node().

  • Then example,in another methor you can use newLayer. Remeber,now newLayer is a child of “this”,“this” is a instance of HelloWorld,
    newLayer only a CCLayer ( I don’t now why you want to creat it as HelloWorld, you only need to use one instance of HelloWorld)
  • If isn’t your object,so please tell what do you want to do,I will help if I can


Ohh….Really thanks, Well explain, I told you that I understand the concept, but I just want to know can i do this

newLayer = HelloWorld::node();//I wanna this layer as a global then how can i make this global…

And want to use this newLayer except “this” in other function….Only for my confusion…otherwisw no problem…All working fine

  • I think maybe you want to creat another instance of helloworld. I think you can’t do it.
    If you create another helloworld in HelloWorld::init(),like hello2. So in hello2::init() must create another helloworld like hello3. And in hello3::init() must create hello4,…
    This will cause your program repeart forever.
    So you can’t create another HelloWorld in . And on HelloWorld::scene(),this is static methor, you only can use static params, not global. In helloWorld::scene(),only one instance of helloworld can be create, that our “this”, you can’t create another
  • Still don’t know what you want to do :D.


Thanks…Problem solved