首页 > 代码库 > cocos2dx 开发成长之路 003
cocos2dx 开发成长之路 003
SpriteBuilder的使用,
创建SpriteBuilder Project,最好和类名一样,或者相关,一个project里面可以有多个file,每个file即是一个UI,这样可以提高UI的重用率,
1,首先创建Project,命名为DressOnEquip,在file菜单里选project settings,可以把默认缩放改为1X,别的都取消勾选
修改好这些设置,done
2,new a file folder,把你要添加的图片资源文件直接托到folder里面,记住,一定要托噢。
3,new a file ,node or layer ,设置好大小
4,设计界面,拖过来拖过去的事~~~
根结点,设置类名:
那个箭头主要写的就是绑定点信息,事件响应相关,比如,按钮
绑定对象都选root target,填写点信息就是绑定的Key string,
设计好界面之后,点击发布,发布完成后,将创建名字和资源文件夹以及project名字一样的文件夹,放在UI下面,方便代码里调用,把生成的spritebuilder文件ccbi文件,放在新建的工程文件夹下,资源放在资源文件夹下。
其实主要用到标记中的文件,其他的好像也没啥用呢。
到此步骤,已经完成spritebuilder方面的工作了,接下来是代码。。
1.new class,,
导入spritebuilder 包。
//// DressOnEquipLayer.h// game1//// Created by NW on 14-6-24.////#ifndef __game1__DressOnEquipLayer__#define __game1__DressOnEquipLayer__#include <iostream>#include "cocos2d.h"#include "cocos-ext.h"#include "SpriteBuilder.h"#include "KKTableView.h"#include "BattleMessageBox.h"#include "DressOnEquipCard.h"#include "Player.h"#include "ItemLayerHandler.h"using namespace cocos2d;using namespace cocos2d::extension;using namespace std;using namespace spritebuilder;const unsigned int TAG_DRESS_ON_EQUIP = 0xF58035D2;class DressOnEquipLayer : public cocos2d::Layer, public spritebuilder::CCBSelectorResolver, public spritebuilder::CCBMemberVariableAssigner, public spritebuilder::NodeLoaderListener{public: ~DressOnEquipLayer(); SB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(DressOnEquipLayer, create); virtual void onNodeLoaded(cocos2d::Node * pNode, NodeLoader * pNodeLoader); virtual bool onAssignCCBMemberVariable(cocos2d::Ref* pTarget, const char* pMemberVariableName, cocos2d::Node* pNode); virtual bool onAssignCCBCustomProperty(cocos2d::Ref* target, const char* memberVariableName, const cocos2d::Value& value); virtual cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::Ref * pTarget, const char* pSelectorName); virtual cocos2d::SEL_CallFuncN onResolveCCBCCCallFuncSelector(cocos2d::Ref * pTarget, const char* pSelectorName); virtual cocos2d::extension::Control::Handler onResolveCCBCCControlSelector(cocos2d::Ref * pTarget, const char* pSelectorName); void onClick(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent); virtual bool onTouchBegan(Touch *pTouch, Event *pEvent); void click(Ref* pSender); void selectClick(Ref* pSender); void cancelClick(Ref* pSender); // void setCardType(DressOnEquipCard::EQUIP_CARD_TYPE type); void changeFuncBtnOutLook(); enum LAYER_TYPE{ DRESS_ON, SINGLE_SELECT, MULTIP_SELECT, }; void setLayerType(LAYER_TYPE type ,ENUM_ITEM_CATEGORY category); private: void onSelectClick(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent); void onCancelClick(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent); void createPlayerHeroBrowser(); CC_SYNTHESIZE(int, m_DressOnType, DressOnType); void resetHeroInformation(); void createHeroCard(); void createMenuOptions(); void showCourse(); void showTutorialInstruction(const char *pMessage, cocos2d::Point pos); void closeNpcTutorialInstruction(Node* pNode); void updateTutorialInstruction(); private: Node* m_pListNode;// KKTableView* m_pEquipList; Sprite* m_pBackground; ControlButton* m_pFuncBtn; ControlButton* m_pCancelBtn; Sprite* m_pExpSprite; Label* m_pTotalExpLabel; Label* m_pUsedCountLabel; //// DressOnEquipCard::EQUIP_CARD_TYPE m_cardType;// // int m_CurrentSelectIndex;// int m_LastSelectIndex;// // vector<PlayerItem> m_ItemsToPass; ItemLayerHandler* m_pItemLayerHandler; };class DressOnEquipLayerLoader : public spritebuilder::LayerLoader {public: SB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(DressOnEquipLayerLoader, loader); protected: SB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(DressOnEquipLayer); };#endif /* defined(__game1__DressOnEquipLayer__) */
//// DressOnEquipLayer.cpp// game1//// Created by NW on 14-6-24.////#include "DressOnEquipLayer.h"#include "DressOnEquipCard.h"#include "SystemHelper.h"#include "constant.h"#include "ServerCommand.h"#include "Player.h"#include "GameData.h"#include "HeroHallMenu.h"#include "GameScene.h"#include "EnhanceEquipLayer.h"#include "LuaHelper.h"#include "Hero.h"#include "DressOnItemHandler.h"#include "SingleSelectItemHandler.h"#include "MultpleSelectItemHandler.h"#define TAG_EQUIP_CARD 1234void DressOnEquipLayer::onNodeLoaded(cocos2d::Node * pNode, NodeLoader * pNodeLoader) { m_pItemLayerHandler = NULL; ignoreAnchorPointForPosition(false); setPosition({DESIGN_SCREEN_WIDTH/2,DESIGN_SCREEN_HEIGHT}); setAnchorPoint({0.5,1}); Node* pBackground = CCLayerColor::create(Color4B(1, 1, 1, 128), DESIGN_SCREEN_WIDTH* 1.5, DESIGN_SCREEN_HEIGHT* 1.5); pBackground->ignoreAnchorPointForPosition(false); pBackground->setPosition({DESIGN_SCREEN_WIDTH/2,DESIGN_SCREEN_HEIGHT/2}); pBackground->setAnchorPoint({0.5, 0.5}); addChild(pBackground,-1); m_pFuncBtn->setTag(2); m_pCancelBtn->setTag(1); m_pBackground->setAnchorPoint(Vec2(0.5,1)); m_pBackground->setPosition(Vec2(getContentSize().width / 2, DESIGN_SCREEN_HEIGHT+300)); m_pBackground->ignoreAnchorPointForPosition(false); m_pBackground->runAction(Sequence::create(MoveTo::create(0.1, Vec2(getContentSize().width / 2,DESIGN_SCREEN_HEIGHT - 25)), MoveTo::create(0.15, Vec2(getContentSize().width/2, DESIGN_SCREEN_HEIGHT+25)),MoveTo::create(0.1, Vec2(getContentSize().width/2,DESIGN_SCREEN_HEIGHT)), NULL)); m_pTotalExpLabel->setSystemFontName(FONT_NUMBER); m_pUsedCountLabel->setSystemFontName(FONT_NUMBER); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(DressOnEquipLayer::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);}bool DressOnEquipLayer::onTouchBegan(Touch *pTouch, Event *pEvent){ return true;}void DressOnEquipLayer::setLayerType(LAYER_TYPE type ,ENUM_ITEM_CATEGORY category){ switch (type) { case DRESS_ON: { m_pItemLayerHandler = new DressOnItemHandler(); m_pItemLayerHandler->setBackgroundSprite(m_pBackground); m_pItemLayerHandler->setListNode(m_pListNode); m_pItemLayerHandler->setExpSprite(m_pExpSprite); m_pItemLayerHandler->setTotalExpLabel(m_pTotalExpLabel); m_pItemLayerHandler->setUsedCountLabel(m_pUsedCountLabel); ((DressOnItemHandler*)m_pItemLayerHandler)->setItemCategory(category); m_pItemLayerHandler->loadItems(); } break; case SINGLE_SELECT: { m_pItemLayerHandler = new SingleSelectItemHandler(); m_pItemLayerHandler->setBackgroundSprite(m_pBackground); m_pItemLayerHandler->setListNode(m_pListNode); m_pItemLayerHandler->setExpSprite(m_pExpSprite); m_pItemLayerHandler->setTotalExpLabel(m_pTotalExpLabel); m_pItemLayerHandler->setUsedCountLabel(m_pUsedCountLabel); m_pItemLayerHandler->loadItems(); } break; case MULTIP_SELECT: { m_pItemLayerHandler = new MultpleSelectItemHandler(); m_pItemLayerHandler->setBackgroundSprite(m_pBackground); m_pItemLayerHandler->setListNode(m_pListNode); m_pItemLayerHandler->setExpSprite(m_pExpSprite); m_pItemLayerHandler->setTotalExpLabel(m_pTotalExpLabel); m_pItemLayerHandler->setUsedCountLabel(m_pUsedCountLabel); m_pItemLayerHandler->loadItems(); } break; default: break; }}void DressOnEquipLayer::changeFuncBtnOutLook(){// switch (m_cardType) {// case DressOnEquipCard::EQUIP_DRESS_ON:// {// Scale9Sprite* sp1 = Scale9Sprite::create("UI/dress_on_equip/equip_normal.png");// Scale9Sprite* sp2 = Scale9Sprite::create("UI/dress_on_equip/equip_down.png");// m_pFuncBtn->setBackgroundSpriteForState(sp1, Control::State::NORMAL);// m_pFuncBtn->setBackgroundSpriteForState(sp2, Control::State::HIGH_LIGHTED);// }// break;// case DressOnEquipCard::EQUIP_SELECT_MULTIPLE:// {// Scale9Sprite* sp1 = Scale9Sprite::create("UI/dress_on_equip/equip_normal.png");// Scale9Sprite* sp2 = Scale9Sprite::create("UI/dress_on_equip/equip_down.png");// m_pFuncBtn->setBackgroundSpriteForState(sp1, Control::State::NORMAL);// m_pFuncBtn->setBackgroundSpriteForState(sp2, Control::State::HIGH_LIGHTED);// }// break;// case DressOnEquipCard::EQUIP_SELECT_SINGLE:// {// Scale9Sprite* sp1 = Scale9Sprite::create("UI/dress_on_equip/equip_normal.png");// Scale9Sprite* sp2 = Scale9Sprite::create("UI/dress_on_equip/equip_down.png");// m_pFuncBtn->setBackgroundSpriteForState(sp1, Control::State::NORMAL);// m_pFuncBtn->setBackgroundSpriteForState(sp2, Control::State::HIGH_LIGHTED);// }// break;// // default:// break;// }}bool DressOnEquipLayer::onAssignCCBMemberVariable(cocos2d::Ref* pTarget, const char* pMemberVariableName, cocos2d::Node* pNode) { SB_MEMBERVARIABLEASSIGNER_GLUE(this, "m_pListNode", Node*, this->m_pListNode); SB_MEMBERVARIABLEASSIGNER_GLUE(this, "m_pFuncBtn", ControlButton*, this->m_pFuncBtn); SB_MEMBERVARIABLEASSIGNER_GLUE(this, "m_pCancelBtn", ControlButton*, this->m_pCancelBtn); SB_MEMBERVARIABLEASSIGNER_GLUE(this, "m_pBackground", Sprite*, this->m_pBackground); SB_MEMBERVARIABLEASSIGNER_GLUE(this, "exp_txt", Label*, this->m_pTotalExpLabel); SB_MEMBERVARIABLEASSIGNER_GLUE(this, "exp_bar", Sprite*, this->m_pExpSprite); SB_MEMBERVARIABLEASSIGNER_GLUE(this, "used_count_label", Label*, this->m_pUsedCountLabel); return false;}bool DressOnEquipLayer::onAssignCCBCustomProperty(cocos2d::Ref* target, const char* memberVariableName, const cocos2d::Value& value) { return false;};cocos2d::SEL_MenuHandler DressOnEquipLayer::onResolveCCBCCMenuItemSelector(cocos2d::Ref * pTarget, const char* pSelectorName) { return NULL;}cocos2d::SEL_CallFuncN DressOnEquipLayer::onResolveCCBCCCallFuncSelector(cocos2d::Ref * pTarget, const char* pSelectorName) { return NULL;};cocos2d::extension::Control::Handler DressOnEquipLayer::onResolveCCBCCControlSelector(cocos2d::Ref * pTarget, const char* pSelectorName){ CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onSelectClick", DressOnEquipLayer::onSelectClick); CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onCancelClick", DressOnEquipLayer::onCancelClick); return NULL;}void DressOnEquipLayer::onCancelClick(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent){ m_pItemLayerHandler->onCancelSelect(); if (m_pItemLayerHandler) { delete m_pItemLayerHandler; m_pItemLayerHandler =NULL; } this->removeFromParent();// cancelClick(sender);}void DressOnEquipLayer::onSelectClick(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent){ m_pItemLayerHandler->onConfirmSelect(); if (m_pItemLayerHandler) { delete m_pItemLayerHandler; m_pItemLayerHandler =NULL; } this->removeFromParent();}DressOnEquipLayer::~DressOnEquipLayer(){ if (m_pItemLayerHandler) { delete m_pItemLayerHandler; m_pItemLayerHandler =NULL; }}
用helper类来读取:
void SystemHelper::setupCCB(){ FileUtils::getInstance()->addSearchPath("UI"); NodeLoaderLibrary * ccNodeLoaderLibrary = NodeLoaderLibrary::getInstance(); ccNodeLoaderLibrary->registerNodeLoader("DressOnEquipLayer", DressOnEquipLayerLoader::loader()); ccNodeLoaderLibrary->registerNodeLoader("DressOnEquipCard", DressOnEquipCardLoader::loader());}Node* SystemHelper::getCCBNode(const char* pPath){ CCBReader* ccbReader = new spritebuilder::CCBReader(NodeLoaderLibrary::getInstance()); Node* pNode = ccbReader->readNodeGraphFromFile(pPath); delete ccbReader; return pNode;}
最后,生成界面:
DressOnEquipLayer* pMenu = static_cast<DressOnEquipLayer*>(SystemHelper::getCCBNode("UI/DressOnEquip/DressOnEquip.ccbi"));// pMenu->setDressOnType(DressOnEquipLayer::DRESS_ON_WEAPON);// pMenu->setCardType(DressOnEquipCard::EQUIP_DRESS_ON); pMenu->setLayerType(DressOnEquipLayer::DRESS_ON,ENUM_ITEM_WEAPON); GameScene::getInstance()->sharedMainUI->getStageUI()->removeChildByTag(TAG_DRESS_ON_EQUIP); pMenu->setTag(TAG_DRESS_ON_EQUIP); GameScene::getInstance()->sharedMainUI->getStageUI()->addChild(pMenu);
cocos2dx 开发成长之路 003
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。