转发,请保持地址:http://blog.csdn.net/stalendp/article/details/8756467
本文主要侧重于CocosBuilder在cocos2dx中的集成。对CocosBuilder使用的介绍比较简单。详细的使用请参考官方教程;
CocosBuilder部分
1)创建工程
首先创建Cocos2dx工程“ShootTheApple”,然后在其Resources目录下新建ccb,并在ccb中创建CocosBuilder工程。目录结构如下图:
CocosBuilder工程会产生3类文件,后缀分别为ccbproj, ccb, ccbi(其中前两个文件使工程文件--ccb.ccbproj, apple.ccb。 ccbi文件,是导出的文件,被cocos2dx程序使用,如上图的apple.ccbi);
2)配置cocosBuilder属性
3)创建ccb文件。
4)编辑ccb文件--按钮
4)编辑ccb文件--动画
代码的集成
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); ccNodeLoaderLibrary->registerCCNodeLoader("Intro", IntroLoader::loader()); /* Create an autorelease CCBReader. */ cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary); /* Read a ccbi file. */ CCNode * node = ccbReader->readNodeGraphFromFile("MainScene.ccbi"); ccbReader->release(); CCScene* scene = CCScene::create(); scene->addChild(node);
CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); ccNodeLoaderLibrary->registerCCNodeLoader("Intro", IntroLoader::loader());IntroLoader对象负责加载相关的类Intro。完整代码如下:
#ifndef ShootTheApple_Intro_h #define ShootTheApple_Intro_h #include "PhysicsSprite.h" #include "CcbBase.h" #include "Playground.h" USING_NS_CC; USING_NS_CC_EXT; class Intro: public CcbBase { public: CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(Intro, create); Intro(){ } virtual ~Intro() { } void btnPlay(cocos2d::CCObject *pSender) { //这个是回调函数, CCDirector::sharedDirector()->replaceScene(PlaygroundLoader::loadCcbi()); } virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, const char * pSelectorName) { CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "btnPlay", Intro::btnPlay); // 把btnPlay对应回调函数,btnPlay的设置在“编辑ccb文件--按钮”中介绍了 } }; /* Forward declaration. */ class CCBReader; class IntroLoader : public cocos2d::extension::CCLayerLoader { public: static CCScene* loadCcbi() { CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary(); ccNodeLoaderLibrary->registerCCNodeLoader("Intro", IntroLoader::loader()); /* Create an autorelease CCBReader. */ cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary); /* Read a ccbi file. */ CCNode * node = ccbReader->readNodeGraphFromFile("MainScene.ccbi"); ccbReader->release(); CCScene* scene = CCScene::create(); scene->addChild(node); return scene; } public: CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(IntroLoader, loader); protected: CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(Intro); }; #endif
#ifndef ShootTheApple_CcbBase_h #define ShootTheApple_CcbBase_h #include "cocos2d.h" #include "cocos-ext.h" #include "Box2D.h" USING_NS_CC; USING_NS_CC_EXT; class CcbBase: public cocos2d::CCLayer , public cocos2d::extension::CCBSelectorResolver , public cocos2d::extension::CCBMemberVariableAssigner , public cocos2d::extension::CCNodeLoaderListener { public: virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, const char * pSelectorName) { // do nothing //CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onPlay", Playground::btnPlay); return NULL; } virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, const char * pSelectorName) { // do nothing return NULL; } virtual bool onAssignCCBMemberVariable(cocos2d::CCObject * pTarget, const char * pMemberVariableName, cocos2d::CCNode * pNode) { // do nothing //CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "apple", CCSprite *, this->apple); return NULL; } virtual bool onAssignCCBCustomProperty(CCObject* pTarget, const char* pMemberVariableName, cocos2d::extension::CCBValue* pCCBValue) { // do nothing return false; } virtual void onNodeLoaded(cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader) { // do nothing } }; #endif
CCReader进行ccbi和自定义类的联系的过程如下,CCBReader一边解析ccbi文件,一边调用自定义类的onResolveCCBCCMenuItemSelector等方法;在这些回调方法中,可以进行相关的绑定工作,如对象绑定,CCMenuItem selector的绑定等。更多的绑定细则,请参考cocosBuilder中的帮助。
版本说明
在这个demo中,cocosBuilder的版本为Version 3 alpha3,配套的cocos2dx版本为cocos2d-2.1rc0-x-2.1.2;
cocosBuilder的官方:http://cocosbuilder.com/
cocosBuilder官方教程:https://github.com/vlidholt/CocosDragon/blob/master/Tutorial/Tutorial.md
cocos2dx的相关页面:http://www.cocos2d-x.org/news/95
源代码下载: http://vdisk.weibo.com/s/BSjUaUQYZZ_MR
源码下载2:链接:http://pan.baidu.com/s/1qYJ4Nnu 密码:g66s