Cocos2d iphone 在HelloWorld项目中将另一层添加到场景中
在cocos2d-x HelloWorld项目中,我尝试将另一层添加到场景中,并在数据成员中保留对该层的引用。由于函数Cocos2d iphone 在HelloWorld项目中将另一层添加到场景中,cocos2d-iphone,cocos2d-x,Cocos2d Iphone,Cocos2d X,在cocos2d-x HelloWorld项目中,我尝试将另一层添加到场景中,并在数据成员中保留对该层的引用。由于函数HelloWorld::scene()是静态的,因此我无法在此函数中添加层(因为我无法设置层的数据成员) 因此,我尝试在init()函数中获取场景,如下所示,但这会导致scene=0x00000000 我做错了什么 bool HelloWorld::init() { bool bRet = false; do { CC_BREAK_IF(
HelloWorld::scene()
是静态的,因此我无法在此函数中添加层(因为我无法设置层的数据成员)
因此,我尝试在init()
函数中获取场景,如下所示,但这会导致scene=0x00000000
我做错了什么
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCScene* scene = NULL;
scene = CCDirector::sharedDirector()->getRunningScene();
// add another layer
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
// set data member
this->layerHud = layerHud;
// next line crashes (because scene is 0x00000000)
scene->addChild(layerHud);
bRet = true;
} while (0);
return bRet;
}
PS:我之所以要将hud层添加到场景中,而不是当前层中,是因为我正在移动当前层,不希望hud层随它一起移动
编辑:由于接受的答案允许多个选项,因此我采取了以下措施来解决问题: 1.)从init()函数中删除HUD层:
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
bRet = true;
} while (0);
return bRet;
}
2.)并将HUD层添加到场景功能中(这也是在cocos2d iphone中实现的方式):
本质上,问题在于我的假设是错误的,“因为函数
HelloWorld::scene()
是静态的,所以我不能在这个函数中添加层(因为我不能为层设置数据成员)。场景是空的,因为在构建第一个场景之前就调用了getRunningScene()
你的一个假设是错误的。HelloWorld::scene()是静态的,但您仍然可以在此函数中添加层(并且可以设置层的数据成员)
正确的方法是创建一个新的公共功能
setLeyerHud(HelloWorldHud* hud);
在HelloWorld类和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(! layer);
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
scene->addChild(layer);
scene->addChild(layerHud);
layer->setLayerHud(layerHud);
} while (0);
这取决于要将Hud添加到HelloWorld层还是要添加到场景中
I.向场景添加新层:
删除代码
CCScene* scene = NULL;
scene = CCDirector::sharedDirector()->getRunningScene();
// add another layer
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
// set data member
this->layerHud = layerHud;
// next line crashes (because scene is 0x00000000)
scene->addChild(layerHud);
从
并在中更改代码
CCScene* HelloWorld::scene()
到
二,。将新层添加到HelloWorld层:
更改代码
CCScene* scene = NULL;
scene = CCDirector::sharedDirector()->getRunningScene();
// add another layer
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
// set data member
this->layerHud = layerHud;
// next line crashes (because scene is 0x00000000)
scene->addChild(layerHud);
在
到
场景在AppDelegate中构建,然后由导演运行。从HelloWorld::scene()中调用HelloWorld::init()。你能解释一下我如何解决这个问题吗?@Ben:我已经编辑了我的答案,你可以试一下。谢谢,这真的很有帮助。你是对的,我的假设是错的。我不能在
scene()
中使用this->layerHUD
,但我仍然可以使用layer->layerHUD
。谢谢!:)
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(! layer);
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
scene->addChild(layer);
scene->addChild(layerHud);
} while (0);
// return the scene
return scene;
CCScene* scene = NULL;
scene = CCDirector::sharedDirector()->getRunningScene();
// add another layer
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
// set data member
this->layerHud = layerHud;
// next line crashes (because scene is 0x00000000)
scene->addChild(layerHud);
HelloWorld::init()
// add another layer
HelloWorldHud* layerHud = HelloWorldHud::create();
CC_BREAK_IF(! layerHud);
// next line crashes (because scene is 0x00000000)
this->addChild(layerHud);