0

I'm having trouble with something that I think is very simple, but i just cant figure it out.

I have a scene named WavePrototypeScene

WavePrototypeScene.h

#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

WavePrototypeScene.cpp

#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->addChild(layer1, 1, 1);

        // add interface layer
        WavePrototypeInterfaceLayer *layer2 = WavePrototypeInterfaceLayer::create();
        CC_BREAK_IF(! layer2);
        scene->addChild(layer2, 2, 2);

    } while (0);

   return scene;
}

then I have my two layers WavePrototypeGameLayer and WavePrototypeInterfaceLayer

WavePrototypeGameLayer.h

#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

WavePrototypeGameLayer.cpp

#include "WavePrototypeGameLayer.h"

bool WavePrototypeGameLayer::init()
{
    bool bRet = false;
    do 
    {
        int health = 10;

    bRet = true;
    } while (0);

    return bRet;
}

WavePrototypeInterfaceLayer.h

#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

WavePrototypeInterfaceLayer.cpp

#include "WavePrototypeInterfaceLayer.h"

bool WavePrototypeInterfaceLayer::init()
{
    bool bRet = false;
    do 
    {
        CCMenuItemImage *actonButtonItem = CCMenuItemImage::create("actionbutton.png", "actionbuttonpressed.png", this, menu_selector(WavePrototypeInterfaceLayer::menuAction));
        actonButtonItem->setPosition(ccp(55, 55));

        CCMenu* actionButtonMenu = CCMenu::create(actonButtonItem, NULL);
        actionButtonMenu->setPosition(CCPointZero);

        this->addChild(actionButtonMenu, 10);

        char text[256];
        sprintf(text, "Health: 0");

    healthLabel = CCLabelTTF::create(text, "Arial", 24);
        healthLabel->setPosition(ccp(100, 100));
    this->addChild(healthLabel, 10);

    bRet = true;
    } while (0);

return bRet;
}

void WavePrototypeInterfaceLayer::menuAction(CCObject* pSender)
{
//button hit
}

my code has a lot more than this, I just cut out everything else for simplicity.

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.

I have tried many things to get this to work and I just cant seem to get it to work correctly.

in cocos2d in objective-C see you can do this by doing WavePrototypegameLayer* interface = (WavePrototypegameLayer*)[self.parent getChildByTag:1];

is there an equivalent to that in cocos2d-x

4

1 回答 1

0
void WavePrototypeInterfaceLayer::menuAction(CCObject* pSender)
{
//button hit
WavePrototypegameLayer* interface = (WavePrototypegameLayer*)(this->getParent()->getChildByTag(1));
//get the value from the layer
}
于 2013-07-16T11:24:37.193 回答