Confusing with Helloworld::scene method

hi, im a student, and just try to make some 2d rpg tiled map like zelda in SNES, etc.
first, i want to tell that i have this code :

//STATIC METHOD
CCScene* HelloWorld::scene()
{
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;
HUDLayer
hudLayer = HUDLayer::create();
CC_BREAK_IF(! hudLayer);

scene~~>addChild;
scene~~>addChild(layer,1);

} while (0);

return scene;
}

all i did is following the steps for adding hudLayer from raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d

i know that the virtual dpad is in hudLayer, and when i press that dpad, i need to send the data back to my Helloworld…

similar like this :
*hud->getData;
the problem is, in raywenderlich tutor, it said :
if ) {
*hud = hudLayer; //i declared HUDLayer* *hud in header file…
and i didnt know how to implement it in c++
many different way i’ve tried :
*hud = new HUDLayer(); << of course it will make a new object, and it will stack with hudLayer; #fail
_hud = hudLayer; << illegal non static member bla bla bla… :3 #fail cause hudLayer is declared in static method
and many more trial and error ways, like deleting the static in HelloWorld header… #fail
also tried pointer to pointer, global scope… #fail
all i want is simple…
i need
_hud have the same values as *hudLayer, so i can access the variable inside that class with same value.

sorry for my bad English… :slight_smile:
just want to learn more and more…

ok, here’s the progress i’ve made…

i saw more and more from that tutorial, and i saw this code in scene method:

ActionLayer layer = initWithHUD:hud] autorelease];
;
return scene;
instead of ActionLayer
layer = ActionLayer::create();
and from that code, i realize that they send hud to init, so they can access it…
what i’ve tried :
HelloWorld
layer = HelloWorld::create()>init; #fail
HelloWorld *layer = HelloWorld::init; <<illegal call of non static member #fail
HelloWorld *layer = HelloWOrld::create()->autorelease
>init; #i feel Dumb~~_~~

in the helloworld class, you create a new method called “bool initWithHUD(HUDLayer _hud)", and "HelloWorldcreateWithHUD(HUDLayer _hud)"
inside the “createWithHUD” function it will be like this more or less :
@
HelloWorld
pRet = new HelloWorld();
if (pRet && pRet~~>initWithHUD) {
pRet~~>autorelease();
} else {
CC_SAFE_DELETE(pRet);
}
return pRet;
@
inside the”initWithHUD" function it will be like this :
@
if ( !CCLayer::init() ) return false;
hudLayer = _hud;
/* the rest of the code /
return true;
@
and in the “HelloWorld::scene()” it will be like this
@
CCScene
scene = CCScene::create();
HUDLayer _hud = HUDLayer::create;
HelloWorld
layer = HelloWorld::createWithHUD(_hud);
scene~~>addChild;
scene~~>addChild(layer, 0);
return scene;
@

Hi FJ,
thanks for your answer, but the problem is if i wrote HelloWorld::createWithHUD(hudLayer) it gives this error
“illegal call of non static member”

but, i solved this…
what i learn is, in static method like scene()
we cant call a variable with “this~~>” or just by call its name…
i didnt know how to say it in english, but…
it looks like this…
in scene :
CCScene* HelloWorld::scene {
CCScene * scene = NULL;
do {
// ‘scene’ is an autorelease object
scene = CCScene::create;
CC_BREAK_IF;
// ‘layer’ is an autorelease object
HelloWorld layer = HelloWorld::create;
CC_BREAK_IF;
HUDLayer
hudLayer = HUDLayer::create;
CC_BREAK_IF;
scene~~>addChild(hudLayer,5);
scene->addChild(layer,1);

**//this~~>hud = hudLayer; << FAIL*
//_hud = hudLayer; << FAIL

//The Correct one that made me confusing for 2-3 days
layer~~>*hud = hudLayer; //This Works
} while (0);
return scene;
}

the best teacher is experience

like I said, you just passed the hudLayer to the createWithHUD() method (sorry not mentioning that this method is static method in my previous post), and the createWithHUD() method (this method is static method) will call the initWithHUD() method (this function IS NOT static). inside the initWithHUD() method, you can use “this->_hud = hudLayer;”.

re-read my code snippet :slight_smile: