Note that there are some explanatory texts on larger screens.

plurals
  1. POCocos2d-x getting data between layers
    text
    copied!<p>I'm having trouble with something that I think is very simple, but i just cant figure it out.</p> <p>I have a scene named WavePrototypeScene</p> <p>WavePrototypeScene.h</p> <pre><code>#ifndef __WAVE_PROTOTYPE_SCENE_H__ #define __WAVE_PROTOTYPE_SCENE_H__ #include "cocos2d.h" #include "WavePrototypeGameLayer.h" #include "WavePrototypeInterfaceLayer.h" using namespace cocos2d; class WavePrototypeScene : public CCScene { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(WavePrototypeScene); }; #endif </code></pre> <p>WavePrototypeScene.cpp</p> <pre><code>#include "WavePrototypeScene.h" CCScene* WavePrototypeScene::scene() { CCScene * scene = NULL; do { scene = CCScene::create(); CC_BREAK_IF(! scene); // add game layer WavePrototypeGameLayer *layer1 = WavePrototypeGameLayer::create(); CC_BREAK_IF(! layer1); scene-&gt;addChild(layer1, 1, 1); // add interface layer WavePrototypeInterfaceLayer *layer2 = WavePrototypeInterfaceLayer::create(); CC_BREAK_IF(! layer2); scene-&gt;addChild(layer2, 2, 2); } while (0); return scene; } </code></pre> <p>then I have my two layers WavePrototypeGameLayer and WavePrototypeInterfaceLayer</p> <p>WavePrototypeGameLayer.h</p> <pre><code>#ifndef __WAVE_PROTOTYPE_GAME_LAYER_H__ #define __WAVE_PROTOTYPE_GAME_LAYER_H__ #include "cocos2d.h" #include "WavePrototypeInterfaceLayer.h" using namespace cocos2d; class WavePrototypeGameLayer : public CCLayer { public: virtual bool init(); static CCScene* scene(); CREATE_FUNC(WavePrototypeGameLayer); }; #endif </code></pre> <p>WavePrototypeGameLayer.cpp</p> <pre><code>#include "WavePrototypeGameLayer.h" bool WavePrototypeGameLayer::init() { bool bRet = false; do { int health = 10; bRet = true; } while (0); return bRet; } </code></pre> <p>WavePrototypeInterfaceLayer.h</p> <pre><code>#ifndef __WAVE_PROTOTYPE_INTERFACE_LAYER_H__ #define __WAVE_PROTOTYPE_INTERFACE_LAYER_H__ #include "cocos2d.h" #include "WavePrototypeGameLayer.h" using namespace cocos2d; class WavePrototypeInterfaceLayer : public CCLayer { public: virtual bool init(); static CCScene* scene(); CREATE_FUNC(WavePrototypeInterfaceLayer); void menuAction(CCObject* pSender); }; #endif </code></pre> <p>WavePrototypeInterfaceLayer.cpp</p> <pre><code>#include "WavePrototypeInterfaceLayer.h" bool WavePrototypeInterfaceLayer::init() { bool bRet = false; do { CCMenuItemImage *actonButtonItem = CCMenuItemImage::create("actionbutton.png", "actionbuttonpressed.png", this, menu_selector(WavePrototypeInterfaceLayer::menuAction)); actonButtonItem-&gt;setPosition(ccp(55, 55)); CCMenu* actionButtonMenu = CCMenu::create(actonButtonItem, NULL); actionButtonMenu-&gt;setPosition(CCPointZero); this-&gt;addChild(actionButtonMenu, 10); char text[256]; sprintf(text, "Health: 0"); healthLabel = CCLabelTTF::create(text, "Arial", 24); healthLabel-&gt;setPosition(ccp(100, 100)); this-&gt;addChild(healthLabel, 10); bRet = true; } while (0); return bRet; } void WavePrototypeInterfaceLayer::menuAction(CCObject* pSender) { //button hit } </code></pre> <p>my code has a lot more than this, I just cut out everything else for simplicity.</p> <p>basically what I want is that when I hit the button on the interface layer, it pulls the int value from the gamelayer instance with the tag 1, and loads it into the label in the interface layer.</p> <p>I have tried many things to get this to work and I just cant seem to get it to work correctly.</p> <p>in cocos2d in objective-C see you can do this by doing WavePrototypegameLayer* interface = (WavePrototypegameLayer*)[self.parent getChildByTag:1];</p> <p>is there an equivalent to that in cocos2d-x</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload