MainScene.h添加新的函数,
CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(MainScene, create);
MainScene.h
中添加一个新类MainSceneLayerLoader
代码如下:
class MainSceneLayerLoader : public cocos2d::extension::CCLayerLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(MainSceneLayerLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(MainScene);
};
.cpp文件
然后我们在MainScene.cpp
中添加一行注册将ccb文件和类绑定起来.
CCNodeLoaderLibrary *lib = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
lib->registerCCNodeLoader("MainScene", MainSceneLayerLoader::loader());
cocosbuilder中将根节点的Custom class
设置为MainScene
.如下图
然后运行下, 确保没有错误.
这几行代码, 就是ccb文件和类绑定的最简模式了.
class MainScene
: public cocos2d::CCLayer
, public cocos2d::extension::CCBSelectorResolver
重写虚函数virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, constchar* pSelectorName);
virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, const char* pSelectorName);
void onMenuItemTest(cocos2d::CCObject *pSender, cocos2d::extension::CCControlEvent pCCControlEvent);
void onButtonTest(cocos2d::CCObject *pSender, cocos2d::extension::CCControlEvent pCCControlEvent);
.cpp文件
SEL_MenuHandler MainScene::onResolveCCBCCMenuItemSelector(cocos2d::CCObject *pTarget, const char*pSelectorName)
{
CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onMenu", MainScene::onMenuItemTest);
return NULL;
}
SEL_CCControlHandler MainScene::onResolveCCBCCControlSelector(cocos2d::CCObject *pTarget, const char*pSelectorName)
{
CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onButtonTest", MainScene::onButtonTest);
return NULL;
}
void MainScene::onMenuItemTest(cocos2d::CCObject *pSender, cocos2d::extension::CCControlEventpCCControlEvent)
{
CCLog("MenuItem");
// this->mLabelText->setString("Hello MenuItem");
}
void MainScene::onButtonTest(cocos2d::CCObject *pSender, cocos2d::extension::CCControlEventpCCControlEvent)
{
CCLog("onButton");
// this->mLabelText->setString("Hello onButton!!!");
}
MenuItem ,Button 是一样的
在cocosbuilder中,在CCB中找到CCControlButton, 并设置其Selector为onButtonTest
. Target设置为Document root
. 如下图
然后运行下, 确保没有错误.
这几行代码, 就是ccb文件和
MenuItem ,Button
绑定的最简模式了.
3 . CCLabelTTF 绑定 -先上代码
.h文件
, public cocos2d::extension::CCBMemberVariableAssigner
, public cocos2d::extension::CCNodeLoaderListener
virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::CCObject * pTarget, constchar* pSelectorName);
virtual cocos2d::extension::SEL_CCControlHandler onResolveCCBCCControlSelector(cocos2d::CCObject * pTarget, const char* pSelectorName);
private:
cocos2d::CCLabelTTF *mLabelText;
.cpp文件
MainScene::MainScene(): mLabelText(NULL)
{
}
MainScene::~MainScene()
{
CC_SAFE_DELETE(mLabelText);
}
bool MainScene::onAssignCCBMemberVariable(cocos2d::CCObject *pTarget, const char*pMemberVariableName, cocos2d::CCNode *pNode)
{
CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mLabelText", CCLabelTTF*, this->mLabelText);
return true;
}
void MainScene::onNodeLoaded(cocos2d::CCNode *pNode, cocos2d::extension::CCNodeLoader *pNodeLoader)
{
this->mLabelText->setString("All Loaded");
}
CCLabelTTF
添加一个Doc root var
类型的绑定mLabelText
. 如下图
然后运行下, 确保没有错误.
这几行代码, 就是ccb文件和
CCLabelTTF
绑定的最简模式了.
最后运行结果,可以把 CCLabelTTF 和 MenuItem,Button连接在一起