cocos2d-x实现cocosbuilder的绑定与菜单按钮功能

在cocosbuilder中新建"MainScene"任意名均可,设置custom class为XJLayer 菜单 按钮CCLABELTTF->

我新手不会讲解那些知识点,直接写出代码,亲测,网上的大神弄出的东西,不是少这个就少那个不敢恭维。我研究了一周也没弄出来,不知道怎么说他们了,坑爹。先弄出成果然后自己研究吧

//XJLayer.h文件


#ifndef __cba__XJLayer__

#define __cba__XJLayer__



#include "cocos2d.h"

#include "cocos-ext.h"

USING_NS_CC;

USING_NS_CC_EXT;


class XJLayer : public CCLayer, public CCBSelectorResolver, public CCBMemberVariableAssigner

{

//static

public:

CREATE_FUNC( XJLayer );

public:

    ~XJLayer()

{

    CC_SAFE_RELEASE_NULL( m_TestLabel );

}

void menuSelectorChangeAccount( CCObject* node );

void controlButtonTest(CCObject* pObject, CCControlEvent event);

//CCBSelectorResolver

virtual SEL_MenuHandler onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName);

    virtual extension::SEL_CCControlHandler onResolveCCBCCControlSelector(CCObject * pTarget, CCString * pSelectorName);

//CCBMemberVariableAssigner

virtual bool onAssignCCBMemberVariable(CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode);

private:

CCLabelTTF* m_TestLabel;

};


class XJLayerLoader : public CCLayerLoader{

public:


CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD( XJLayerLoader, loader );


CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD( XJLayer );

};



#endif /* defined(__ConnectToCustomClass__XJLayer__) */


//.cpp


#include "XJLayer.h"

#include "main.h"

void XJLayer::menuSelectorChangeAccount( CCObject* pObject )

{

CCLog( "CGStartGameLayer::menuSelectorChangeAccount" );

    

CCScene* scene = Main::scene();

   

   

    CCDirector::sharedDirector()->replaceScene(scene);


}

void XJLayer::controlButtonTest(CCObject* pObject, CCControlEvent event)

{

   this->m_TestLabel->setString("Hello CocosBuilder!!!");

    

    CCLog( "CGStartGameLayer::controlButtonEventhandle" );

}


SEL_MenuHandler XJLayer::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName)

{

CCB_SELECTORRESOLVER_CCMENUITEM_GLUE( this, "menuSelectorChangeAccount", XJLayer::menuSelectorChangeAccount);

return NULL;

}




extension::SEL_CCControlHandler XJLayer::onResolveCCBCCControlSelector(CCObject * pTarget, CCString * pSelectorName)

{

CCB_SELECTORRESOLVER_CCCONTROL_GLUE( this, "controlButtonTest", XJLayer::controlButtonTest );

return NULL;

}


bool XJLayer::onAssignCCBMemberVariable(CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode)

{

CCB_MEMBERVARIABLEASSIGNER_GLUE( this, "m_TestLabel", CCLabelTTF*, m_TestLabel );

return false;

}

修改

AppDelegate.cpp

#include "cocos2d.h"

#include "cocos-ext.h"

#include "XJLayer.h"


bool AppDelegate::applicationDidFinishLaunching()

{

    // initialize director

    CCDirector *pDirector = CCDirector::sharedDirector();

    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());


    // turn on display FPS

    pDirector->setDisplayStats(true);


    // set FPS. the default value is 1.0/60 if you don't call this

    pDirector->setAnimationInterval(1.0 / 60);

    CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary()->registerCCNodeLoader( "XJLayer", XJLayerLoader::loader());

    CCBReader* reader = new CCBReader( CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary() );

    reader->autorelease();


    // run

    pDirector->runWithScene( reader->createSceneWithNodeGraphFromFile( "MainScene.ccbi" ) );


    return true;

}

实现菜单跳转场景需要另外新建一个场景。。。完事 

本人新手,有问题我能回答的也一定相告,大牛们都忙,不忙的也弄出一些坑爹的文章,再说就想骂人了 。。。闪


你可能感兴趣的:(新手笔记)