我遇到了一些我认为很简单的问题,但我无法弄明白。
我有一个名为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;
}
然后我有两层WavePrototypeGameLayer和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
}
我的代码远不止这些,为了简单起见,我只是删掉了其他所有内容。
基本上我想要的是当我点击界面图层上的按钮时,它会从游戏层实例中使用标记1拉取int值,并将其加载到界面图层中的标签中。
我已经尝试了许多方法来实现这一点,而我似乎无法让它正常工作。
目标-C中的cocos2d中的看你可以这样做 WavePrototypegameLayer * interface =(WavePrototypegameLayer *)[self.parent getChildByTag:1];
与cocos2d-x
相同答案 0 :(得分:0)
void WavePrototypeInterfaceLayer::menuAction(CCObject* pSender)
{
//button hit
WavePrototypegameLayer* interface = (WavePrototypegameLayer*)(this->getParent()->getChildByTag(1));
//get the value from the layer
}