cocosbuilder for cocos2d-x code connection

转载地址:http://yui.co/cocosbuilder-for-cocos2dx-usage-part-one-code-connection/

I. create a empty HelloCocos2dx project

use the cocos2dx template

II. setup CocosBuilder

  1. create a CocosBuilder project called HelloCocos2dx
  2. in CocosBuilder File->new File to create a Layer file HelloLayer
  3. add a CCLabelTTF,make it center,in the property panel Code Connections section change Don’t assign to Owner var,and input txtHello,change Sample Text to “Hello From CocosBuilder”
  4. add a CCMenu,and add a CCMenuItemImage to the CCMenu,in the CCMenuItemImage property panel CCMenuItem section,input onSayHello: for Selector field and change target to Owner,in the CCMenuItemImage section select a image for normal state.
  5. in CocosBuilder File->Project Settings set Publish Directory to xcode project Resources/UI,uncheck all the checkbox,now publish

III. connect with code

  1. create a cpp class called LayerHello
    LayerHello.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    #ifndef __LayerHello__
    #define __LayerHello__
    #include "cocos2d.h"
    #include "CCBMemberVariableAssigner.h"
    #include "CCBSelectorResolver.h"
    #include "CCLabelTTF.h"USING_NS_CC_EXT;
    class LayerHello  :  public cocos2d :: CCLayer, public CCBMemberVariableAssigner, public CCBSelectorResolver
    {
    public :

    //================UI injections
    CCLabelTTF  *txtHello ;
    void onSayHello (CCObject  *sender ) ;

    //================

    virtual  bool init ( ) ;

    virtual  bool onAssignCCBMemberVariable (CCObject  * pTarget, cocos2d :: CCString  * pMemberVariableName, CCNode  * pNode )
    {
    CCB_MEMBERVARIABLEASSIGNER_GLUE ( this, "txtHello", CCLabelTTF,txtHello ) ;
    return  false ;
    }
    virtual cocos2d :: SEL_MenuHandler onResolveCCBCCMenuItemSelector (CCObject  * pTarget, CCString  * pSelectorName )
    {
    CCB_SELECTORRESOLVER_CCMENUITEM_GLUE ( this, "onSayHello:",LayerHello :: onSayHello ) ;
    return  NULL ;
    }
    virtual cocos2d :: extension :: SEL_CCControlHandler onResolveCCBCCControlSelector (CCObject  * pTarget, CCString  * pSelectorName )
    {
    return  NULL ;
    }

    CREATE_FUNC (LayerHello ) ;
    } ;
    #endif /* defined(__LayerHello__) */

    LayerHello.cpp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    #include "LayerIncActorInfo.h"
    #include "CCBReader.h"
    #include "CCNodeLoaderLibrary.h"

    LayerHello ::  LayerHello ( )
    : txtHello ( 0 )
    {

    }

    bool LayerHello :: init ( )
    {
    bool bRet  =  false ;
    do
    {
    CC_BREAK_IF ( ! CCLayer :: init ( ) ) ;

    CCNodeLoaderLibrary  * ccNodeLoaderLibrary  = CCNodeLoaderLibrary :: sharedCCNodeLoaderLibrary ( ) ;
    CCBReader  * ccbReader  =  new CCBReader (ccNodeLoaderLibrary, this, this ) ;
    CCNode  * node  = ccbReader - >readNodeGraphFromFile ( "UI/""HelloLayer.ccbi"this ) ;
    this - >addChild (node ) ;

    bRet  =  true ;
    }  while  ( 0 ) ;

    return bRet ;
    }

    void LayerHello :: onSayHello (CCObject  *sender )
    {
    txtHello - >setString ( "Hello From Code" ) ;
    }
  2. add LayerHello to your scene,click run to test it
  3. in you game,click the CCMenuItemImage,the label should change to “Hello From Code”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值