1.我使用的是 cocos2dx-2.03 的版本
cocosbuilder 2.1 的版本
如果版本不兼容,会出现类似 WARNING! Incompatible ccbi file version (file: 3 reader: 5) 这样的警告.
2.需要注意cocosbuilder的基本使用,注意文件的路径等
代码加载一个Layer:
void FirstScene::onEnter() { CCScene::onEnter(); // 创建一个 CCNodeLoaderLibrary CCNodeLoaderLibrary* ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); ccNodeLoaderLibrary->registerCCNodeLoader("FirstLayer",FirstLayerLoader::loader()); // 创建一个 CCBReader CCBReader* ccbReader = new CCBReader(ccNodeLoaderLibrary); //Read a ccbi file CCNode* node = ccbReader->readNodeGraphFromFile("ccb_publish/first.ccbi", this); ccbReader->release(); if (NULL != node) { this->addChild(node); } }
这些,可以在一个Scene的 onEnter()里面去完成。
注意里面的
FirstLayer 和 FirstLayerLoader 这两个类.
FirstLayerLoader这个类需要继承自CCLayerLoader,每一个Document Root这样的CCLayer都需要对应一个单独的XXXLayerLoader.
否则,事件就不能绑定上去,会出现类似 Skipping selector 'playBtnClicked' since no CCBSelectorResolver is present.这样的错误.
class FirstLayerLoader : public CCLayerLoader { public: CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(FirstLayerLoader, loader); protected: CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(FirstLayer); };
class FirstLayer :public CCLayer ,public CCBSelectorResolver ,public CCBMemberVariableAssigner ,public CCNodeLoaderListener { public: CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(FirstLayer, create); FirstLayer(); virtual ~FirstLayer(); virtual void onEnter(); // // CCBSelectorResolver virtual SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName); virtual SEL_CCControlHandler onResolveCCBCCControlSelector(CCObject * pTarget, CCString * pSelectorName); // // CCBMemberVariableAssigner virtual bool onAssignCCBMemberVariable(CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode); // //CCNodeLoaderListener virtual void onNodeLoaded(CCNode * pNode, CCNodeLoader * pNodeLoader); // // Btn Clicked void playBtnClicked(CCObject* pSender,CCControlEvent pCCControlEvent); };
Cocosbuilder里面添加CCControlButton的设置: