cocosBuilder

cocosBuilder是一款很好用的cocos2d辅助开发工具。

网上的教程很多,但新版本的使用略有出入。我记一下。

下载下了工具后,新工程,File->New->New Project,一般后提示一个出错消息:too many directories.

选择:File->update File in directory,把工程路径选到xcode工程的Resource目录。

按照网上教程建好后。

选择File->publish,把生成的ccbi文件拷贝到工程。

使用如下代码使用。

    CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();

    ccNodeLoaderLibrary->registerCCNodeLoader(“TestCocosBuilder”,TestCocosBuilderLoader::loader());//这个是用来响应事件的,如果只是显示可以不用这个

    

    //创建一个自动释放的CCB读取类(CCBReader头文件"cocos-exh"

    cocos2d::extension::CCBReader * ccbReader = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary);

    ccbReader->autorelease();

    

    //读取ccbi文件获取根节点

    CCNode * node = ccbReader->readNodeGraphFromFile("helloworldlayer.ccbi");

    

    //将根节点CCLayer加入到场景中

    CCScene * scene = CCScene::create();

    if(node != NULL) {

        scene->addChild(node);

    }

    //切换到cbbi生成的场景

    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.5f, scene, ccc3(0, 0, 0)));

TestCocosBuilderLoader类,加载使用

#ifndef CocosBuilderTest_TestCocosBuilderLoader_h
#define CocosBuilderTest_TestCocosBuilderLoader_h

#include "TestCocosBuilder.h"

class CCBReader;

class TestCocosBuilderLoader : public cocos2d::extension::CCLayerLoader {
public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(TestCocosBuilderLoader, loader);
    
protected:
    CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(TestCocosBuilder);
};

#endif
TestCocosBuilder类,响应事件

#ifndef __CocosBuilderTest__TestCocosBuilder__
#define __CocosBuilderTest__TestCocosBuilder__

#include "cocos2d.h"
#include "cocos-ext.h"

using namespace cocos2d;
using namespace extension;

class TestCocosBuilder:public cocos2d::CCLayer
    , public cocos2d::extension::CCBSelectorResolver
    , public cocos2d::extension::CCBMemberVariableAssigner {

public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(TestCocosBuilder, create);
    
    //CCBSelectorResolver重写函数如下
    virtual SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName);
    virtual SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName);
        
    //CCItemImage 回调函数
    void menu_backfunc(cocos2d::CCObject * pSender);
        
    //CCBMemberVariableAssigner重写函数如下
    virtual bool onAssignCCBMemberVariable(CCObject * pTarget, cocos2d::CCString * pMemberVariableName, CCNode * pNode);

    CCLabelTTF* cbbiTTF;
    
};


#endif /* defined(__CocosBuilderTest__TestCocosBuilder__) */


#include "TestCocosBuilder.h"

SEL_MenuHandler TestCocosBuilder::onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName){
    //将ccbi中的“menu_backfunc”回调函数映射成本类的menu_backfunc函数
    CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "menu_backfunc", TestCocosBuilder::menu_backfunc);

    return NULL;
}

SEL_CCControlHandler TestCocosBuilder::onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName){

    return NULL;
}


bool TestCocosBuilder::onAssignCCBMemberVariable(CCObject * pTarget, cocos2d::CCString * pMemberVariableName, CCNode * pNode){
    //将ccbi中的“cbbiTTF”映射成本类的cbbiTTF对象
    CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "cbbiTTF", CCLabelTTF *, this->cbbiTTF);
    
    return false;
}


void TestCocosBuilder::menu_backfunc(cocos2d::CCObject * pSender){
    if(cbbiTTF!=NULL){ 
        cbbiTTF->setString("MenuItemImage 事件被触发");
 

随便说一句:cocostudio 是一款和cocosBuilder类似的工具。但估计以后cocos2dx开发,会多用到cocostudio。cocostudio又是在模仿unity3d。它的很多使用习惯和unity3d相接近。

你可能感兴趣的:(cocosBuilder)