加载ccbi,返回CCNode
CCNode* LoadCCBIFile(
const char* ccbiFileName,
CCObject *pOwner,
const char* ccbiRootPath= NULL
)
{
CCBReader* pReader = new CCBReader(CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary());
pReader->autorelease();
//设置ccbi的目录
if (ccbiRootPath)
{
pReader->setCCBRootPath(ccbiRootPath);
}
//加载ccbi
CCNode* pNode = pReader->readNodeGraphFromFile(ccbiFileName,pOwner);
return pNode;
}
连接自己定义类
一、在CocosBuilder中绑定类和成员
1.创建一个CCLayer
2.在Document->JavaScript Controller勾去掉,就可以设置C++自定义类。
3.在右边Code connetctions中,Custom Class输入定义的类名。
4.选择Doc Root Var指定为成员变量
5.为CCMenu或CCControlButton指定回调函数。在selector中输入回调函数,Target指定为
Document root。
二、代码连接
1.为ccbi指定一个CCLayerLoader,CCLayerLoader只需要指定一个静态方法创建CCLayerLoader对
象,还需要一个静态方法创建ccbi连接的类
class MyLayerLoader
: public CCLayerLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(MyLayerLoader);
CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(HelloWorld);
};
#define CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(T, METHOD) static T * METHOD() { \
T * ptr = new T(); \
if(ptr != NULL) { \
ptr->autorelease(); \
return ptr; \
} \
CC_SAFE_DELETE(ptr); \
return NULL; \
}
#define CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(T, METHOD)\
static T * METHOD() { \
T * ptr = new T(); \
if(ptr != NULL && ptr->init()) { \
ptr->autorelease(); \
return ptr; \
} \
CC_SAFE_DELETE(ptr); \
return NULL; \
}
2.设计ccbi连接的类,首先必须继承于CCLayer和CCBMemberVariableAssigner,如果cocosBuilder
使用了selector,还需要继承CCBSelectorResolver类。
class HelloWorld
: public CCLayer,
public CCBMemberVariableAssigner,
public CCBSelectorResolver
{
public:
CCB_DECL(HelloWorld); //在最后定义
HelloWorld();
~HelloWorld();
//CCBMemberVariableAssigner中的虚函数
virtual bool onAssignCCBMemberVariable(CCObject* pTarget,
const char* pMemberVariableName, CCNode* pNode);
virtual bool onAssignCCBCustomProperty(CCObject* pTarget,
const char* pMemberVariableName, CCBValue* pCCBValue);
//CCBSelectorResolver中需要实现的函数
virtual SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject * pTarget,
const char* pSelectorName){return NULL;}
virtual SEL_CCControlHandler onResolveCCBCCControlSelector(CCObject * pTarget,
const char* pSelectorName);
private:
CCControlButton* m_pTestBtn;
};
3.实现CCBMemberVariableAssigner::onAssignCCBMemberVariable为成员赋值,
实现CCBSelectorResolver::onResolveCCBCCMenuItemSelector和
CCBSelectorResolver::onResolveCCBCCControlSelector为CCmenuItem和CCControlButton指
定回调函数。
bool KLRoleCardLayer::onAssignCCBMemberVariable( CCObject* pTarget,
const char* pMemberVariableName, CCNode* pNode )
{
//第二个参数是cocosBuider中自定的名字
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, KL_TOSTRING(m_pTestBtn),
CCControlButton* ,m_pTestBtn);
return false;
}
cocos2d::extension::SEL_CCControlHandler HelloWorld::onResolveCCBCCControlSelector(
CCObject * pTarget, const char* pSelectorName )
{
CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "OnPressButton", HelloWorld::OnClickBtn);
return NULL;
}
//在cocosBuilder添加int,float,string,bool类型的成员通过这个函数赋值
bool HelloWorld::onAssignCCBCustomProperty(CCObject* pTarget,
const char* pMemberVariableName, cocos2d::extension::CCBValue* pCCBValue)
{
bool bRet = false;
if (pTarget == this)
{
if (0 == strcmp(pMemberVariableName, "mCustomPropertyInt"))
{
this->mCustomPropertyInt = pCCBValue->getIntValue();
bRet = true;
}
else if ( 0 == strcmp(pMemberVariableName, "mCustomPropertyFloat"))
{
this->mCustomPropertyFloat = pCCBValue->getFloatValue();
bRet = true;
}
else if ( 0 == strcmp(pMemberVariableName, "mCustomPropertyBoolean"))
{
this->mCustomPropertyBoolean = pCCBValue->getBoolValue();
bRet = true;
}
else if ( 0 == strcmp(pMemberVariableName, "mCustomPropertyString"))
{
this->mCustomPropertyString = pCCBValue->getStringValue();
bRet = true;
}
}
return bRet;
}
注意:构造函数中必须将需要赋值的变量赋值为pTestBtn = NULL,否则 onAssignCCBMemberVariable将赋值失败。同时,在析构函数中CC_SAFE_RELEASE_NULL(m_pTestBtn);来释放资源。
三、整理
#define TOSTRING(val) #val
#define TO_CCOBJECT(other) ((CCObject*)other)
#define REGISTER_CCNODELOADER(className)\
CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary()->\
registerCCNodeLoader(#className, className::className##Loader::loader());
#define UNREGISTER_CCNODELOADER(className)\
CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary()->unregisterCCNodeLoader(#className);
#define REGISTER_KLCLASS()\
REGISTER_CCNODELOADER(CSkillDescLayer);\
REGISTER_CCNODELOADER(CAttrPanelLayer);\
REGISTER_CCNODELOADER(CSquadLayer);
#define UNREGISTER_KLCLASS()\
NREGISTER_CCNODELOADER(CSkillDescLayer);\
UNREGISTER_CCNODELOADER(CAttrPanelLayer);\
UNREGISTER_CCNODELOADER(CSquadLayer);
#define CREATE_FUNC_CCB(className)\
static className* create(const char* szCCBIName, CCObject *pOwner)\
{\
CCBReader* pReader = new CCBReader(CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary());\
char szCCBi[MAX_SIZE];\
sprintf(szCCBi,"%s/%s",CCB_ROOTPATH,szCCBIName);\
className* pNode = (className*)(LoadCCBIFile(szCCBi,pOwner,CCB_ROOTPATH));\
return pNode;\
}
#define CCB_LOADER_DECL(className)\
public:\
class className##Loader : public CCLayerLoader\
{\
public:\
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(className::className##Loader, loader);\
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(className);\
};
#define CCB_DECL(className)\
CCB_LOADER_DECL(className);\
CREATE_FUNC(className);\
CREATE_FUNC_CCB(className);