4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2dx v3 with cocosbuilder

Last updated at Posted at 2015-01-27

概要

  • cocos2dxの3.3でcocosBuilderの対応関係のメモ
  • cocos2dxの2.xでしかcocosBuilder使ったことなかったのでハマった

環境

基本確認

  • とりあえずCCがとれたらしい...
  • CCNode → Node
  • CCLayer → Layer
  • 以下略。

cocosBuilder側の操作

  • rootNode(Layer?)にcustomClassを設定。今回はOpeningLayerにした
  • buttonとか設置
  • buttonのcallbackセレクタとか設定。document Rootにアタッチ。
  • Sprite(画像)とか設置
  • code connectionでdoc root varとnameを設定。

Loaderクラスの作成

  • OpeningLayerLoader.h
  • CocosBuilder.hのincludeがよく分かってない... とりあえず階層全部のせたらうまくいった...
#include "cocos2d.h"
#include "cocos-ext.h"
#include "OpeningLayer.h"
#include "editor-support/cocosbuilder/CocosBuilder.h"

class OpeningLayerLoader: public cocosbuilder::LayerLoader {
public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(OpeningLayerLoader, loader);
    
    CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(OpeningLayer);
};

OpeningLayerクラスの作成

  • OpeningLayer.h
#include "cocos2d.h"
#include "cocos-ext.h"
#include "cocosbuilder/CocosBuilder.h"
USING_NS_CC;
USING_NS_CC_EXT;

class OpeningLayer:
public cocos2d::Layer,
public cocosbuilder::CCBMemberVariableAssigner,
public cocosbuilder::CCBSelectorResolver
{
public:
    static Scene* createScene();
    OpeningLayer();
    ~OpeningLayer();
    CREATE_FUNC(OpeningLayer);
    
    bool init(){return true;};
    
    bool onAssignCCBMemberVariable(cocos2d::Ref* pTarget, const char* pMemberVariableName, cocos2d::Node* pNode);
    cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
    { return nullptr;};
    Control::Handler onResolveCCBCCControlSelector(cocos2d::Ref * pTarget, const char* pSelectorName);
    cocos2d::SEL_CallFuncN onResolveCCBCCCallFuncSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
    { return NULL; };
    
    void onPushButton(Ref* pSender, cocos2d::extension::Control::EventType event);
    void onPushReplaceScene(Ref* pSender, cocos2d::extension::Control::EventType event);
private:
    Sprite *_image;
};

  • OpeningLayer.cpp
  • 長くなるのでcocosbuilder関係のとこだけ。
bool OpeningLayer::onAssignCCBMemberVariable(cocos2d::Ref *pTarget, const char *pMemberVariableName, Node *pNode)
{
    CCB_MEMBERVARIABLEASSIGNER_GLUE_WEAK(this, "image", Sprite*, _image);
    return false;
}

cocos2d::extension::Control::Handler OpeningLayer::onResolveCCBCCControlSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
{
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "pushButton", OpeningLayer::onPushButton);
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "replaceScene", OpeningLayer::onPushReplaceScene);
    return nullptr;
}

とりあえずこれで動いた。

読み込み

cocosbuilder::NodeLoaderLibrary* pNodeLoaderLibrary = cocosbuilder::NodeLoaderLibrary::getInstance();
pNodeLoaderLibrary->registerNodeLoader("OpeningLayer", OpeningLayerLoader::loader());
cocosbuilder::CCBReader* pReader = new cocosbuilder::CCBReader(cocosbuilder::NodeLoaderLibrary::getInstance());
Node *n = pReader->readNodeGraphFromFile("OpeningLayer.ccbi");
  • 必要に応じてcastしてもよい。

参考文献

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?