新人关于init()的问题

我一直理解的init()就是类似构造函数的,今天做画面切换,新建立一个CCScene类,但是好像init()没有被执行,请问会有什么原因造成呢?谢谢
#include “GameLayer.h”
using namespace cocos2d;
GameLayer::GameLayer(){
}
GameLayer::~GameLayer(){}
// on “init” you need to initialize your instance
bool GameLayer::init()
{
bool bRet = false;
do
{
//CC_BREAK_IF(!CCLayer::init());
//CCSprite *bg=CCSprite::spriteWithFile(“HelloWorld.png”);
//CC_BREAK_IF(!bg);
//bg->setAnchorPoint(CCPointZero);
//bg->setOpacity(100);//背景色彩变淡
//this->addChild(bg,0,1);
CCMenuItemFont::setFontName(“Marker Felt”);
CCMenuItemFont::setFontSize(25);
CCMenuItemFont *newGame = CCMenuItemFont::itemFromString(“NEW GAME”);
CC_BREAK_IF(!newGame);
CCMenuItemFont *lodeGame=CCMenuItemFont::itemFromString(“Lode”);
CC_BREAK_IF(!lodeGame);
CCMenuItemFont *gameSettings= CCMenuItemFont::itemFromString(“Option”);
CC_BREAK_IF(!gameSettings);
CCMenuItemFont *quit=CCMenuItemFont::itemFromString(“Qiut”);
CC_BREAK_IF(!quit);
CCMenu *menu=CCMenu::menuWithItems(newGame,lodeGame,gameSettings,quit,NULL);
CC_BREAK_IF(!menu);
menu->alignItemsVertically();
this->addChild(menu,1);

}while(0);

return bRet;

}

在 this->addChild(menu,1);下面加上 bRet=true;

你应该看看HelloWorld.cpp先

你有调用到init()吗?

你???名字叫 GameLayer, 那到底是CCLayer ?是 CCScene?

如果你是用了LAYER_NODE_FUNC(GameLayer), 那? GameLayer::node() ? init() 就?被呼叫.

init()不是自动调用的,你要手动调用。

init()方法 需要使用cocos2d的几个静态方法的时候才会调用到 比如CCNode::node(),CCScene()::scene(),而且必须return true;才能构建成功,自己new的就需要自己去调用了

init算是一种二段构造吧,其实工作完全可以挪到构造函数中,只是若在构造函数中发生内存不足或者一些加载失败的情况,代码崩在构造函数中就不好了,因为就不会再调用析构函数了,造成了内存泄露。一般框架中的init都如以上大神们说的那样,需要自己调用。这样能够增加代码的健壮性。

如果你是用node初始化这个类的话 还要加上LAYER_NODE_FUNC(youClass),在.h中

在 this->addChild(menu,1);下面加上 bRet=true;

你应该看看HelloWorld.cpp先

你有调用到init()吗?

你???名字叫 GameLayer, 那到底是CCLayer ?是 CCScene?

如果你是用了LAYER_NODE_FUNC(GameLayer), 那? GameLayer::node() ? init() 就?被呼叫.

init()不是自动调用的,你要手动调用。

init()方法 需要使用cocos2d的几个静态方法的时候才会调用到 比如CCNode::node(),CCScene()::scene(),而且必须return true;才能构建成功,自己new的就需要自己去调用了