cocos2d-x绑定这个狗屎的cocosbuilder的方法!!!
狗屎的cocosbuilder,连个说明文档都没有。网上一堆超级狗屎的所谓文档,他妈的都是你抄我,我抄你,脑都没用的。最后,要老子我看CCBReader源码才搞明白。
先把过程写出来。
在cocosbuilder一个button,添加个事件:selector = appleClicked:
但是在cocos2d-x死活绑定不上去。你麻痹的,搞了我一天,最终决定看源码。网上的资料都是放屁。
先放出cocos2d-x的代码:
// Page1Scene.h
// demo02
//
// Created by chen zhang on 12-11-17.
//
//
#ifndef __demo02__Page1Scene__
#define __demo02__Page1Scene__
#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace cocos2d::extension;
class Page1Scene : public cocos2d::CCScene, public CCBAnimationManagerDelegate
{
CCBAnimationManager *animationManager;
virtual bool init();
virtual void completedAnimationSequenceNamed(const char *name);
void setBlendFuncToAllChildren(CCNode *node, const ccBlendFunc &blend);
~Page1Scene();
public:
static cocos2d::CCScene* scene();
CREATE_FUNC(Page1Scene);
};
#endif /* defined(__demo02__Page1Scene__) */
// Page1Scene.cpp
// demo02
//
// Created by chen zhang on 12-11-17.
//
//
#include "Page1Scene.h"
#include "Page1Layer.h"
using namespace cocos2d;
using namespace cocos2d::extension;
CCScene* Page1Scene::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
Page1Scene *layer = Page1Scene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool Page1Scene::init()
{
CCNodeLoaderLibrary *ll = CCNodeLoaderLibrary::newDefaultCCNodeLoaderLibrary();
ll->registerCCNodeLoader("Page1Layer", Page1BuilderLoader::loader());
CCBReader *ccbReader = new CCBReader(ll);
animationManager = NULL;
CCNode *node = ccbReader->readNodeGraphFromFile("HelloCocosBuilder.ccbi", this, &animationManager);
animationManager->setDelegate(this);
// animationManager->runAnimations("ArmPigForeground");
ccbReader->release();
ccBlendFunc normalBlend = (ccBlendFunc){ GL_ONE, GL_ONE_MINUS_CONSTANT_ALPHA};
if(node != NULL)
{
setBlendFuncToAllChildren(node, normalBlend);
this->addChild(node);
}
return true;
};
void Page1Scene::setBlendFuncToAllChildren(cocos2d::CCNode *node, const ccBlendFunc &blend)
{
CCArray* children = node->getChildren();
CCObject* current = NULL;
CCARRAY_FOREACH(children, current)
{
CCNode* child = dynamic_cast<CCNode *>(current);
if (child)
{
CCSprite* sprite = dynamic_cast<CCSprite *>(current);
if (sprite && sprite->getTag() == 50)
{
sprite->setBlendFunc(blend);
}
setBlendFuncToAllChildren(child, blend);
}
}
};
void Page1Scene::completedAnimationSequenceNamed(const char *name)
{
}
Page1Scene::~Page1Scene()
{
}
这段像屎一样的代码,就是初始化了一个scene场景,然后添加了一个layer。把ccbi文件读进来,放到layer。
//
// demo02
//
// Created by chen zhang on 12-11-17.
//
//
#ifndef __demo02__Page1Layer__
#define __demo02__Page1Layer__
#include <iostream>
#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace cocos2d::extension;
class Page1Layer : public CCLayer, public CCBSelectorResolver
{
virtual bool init();
virtual SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject *pTarget, CCString *pSelectorName);
virtual SEL_CCControlHandler onResolveCCBCCControlSelector(CCObject *pTarget, CCString *pSelectorName);
void appleClicked(CCObject *pSender, CCControlEvent pCCControlEvent);
public:
CREATE_FUNC(Page1Layer);
};
class Page1BuilderLoader : public CCLayerLoader
{
public:
CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(Page1BuilderLoader, loader);
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(Page1Layer);
};
#endif /* defined(__demo02__Page1Layer__) */
// Page1Layer.cpp
// demo02
//
// Created by chen zhang on 12-11-17.
//
//
#include "Page1Layer.h"
using namespace cocos2d;
using namespace extension;
bool Page1Layer::init()
{
if(!CCLayer::init())
return false;
return true;
}
SEL_MenuHandler Page1Layer::onResolveCCBCCMenuItemSelector(CCObject *pTarget, CCString *pSelectorName)
{
CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "appleClicked:", Page1Layer::appleClicked);
return NULL;
};
void Page1Layer::appleClicked(CCObject *pSender, CCControlEvent pCCControlEvent)
{
CCLOG("button clicked");
};
SEL_CCControlHandler Page1Layer::onResolveCCBCCControlSelector(cocos2d::CCObject *pTarget, cocos2d::CCString *pSelectorName)
{
CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "appleClicked:", Page1Layer::appleClicked);
return NULL;
};
这段像狗屎一样的代码,最主要就是继承了一个CCBSelectorREsolver,把界面的selector绑定到代码。
但是你妈的死活帮不上去。最后看代码,分析如下:
cocosbuilder 源码解析
CCBReader -> readNodeGraphFromFile
读取二进制文档
this->readNodeGraphFromData
mActionManager 动画控制器,控制读取的CCNODE
boolCCBReader::initWithData(CCData *pData, CCObject *pOwner)
初始化animationManager
CCBReader -> raedFileWithlearUP
CCNode *pNodeGraph = readFileWithCleanUp(true);
判断头版本
获取字符串缓存
获取动作序列
CCNode *pNode = readNodeGraph();
这个方法是个递归,读取一个className,搞完了,下一个。
有了className,就从LIbrary获取CCNodeLoader, 这个loader就是外部设置的Page1BuilderLoader
通过loader 加载了自定义类的Page1Layer
在读取property的时候,就会初始化绑定方法,这里会指向targetTYpeDocumentRoot / targetTypeOwner
这个就是在页面的那个设置。
root = animationManager.rootNode = Page1Layer
owner = pccbReader.owner = 外部read的指针this = Page1Seene
分割线--------
看懂的牛逼们,应该知道问题了,就是设置button的selector的owner,要设置为document root,这样才会调用layer作为resolver,然后才能调用对应的解析器代码。
我草。网上一群装逼的二货,不是少这个,就是少那个,还说 看 TestCPP很简单。我怀疑你到底懂不懂的。