标签:cocos2d-x3.4 游戏 华容道
角色类设计:
曹操:4个格子
将军_横:2个
将军_竖:2个
兵:1个
将角色分为12种,因为,其中10种是将军,横向的5种,竖向的5种,1种兵,1种Boss
角色属性:
ID:
Type:
贴图:
Role.h
#ifndef _ROLE_H_ #define _ROLE_H_ #include "cocos2d.h" #include "tinyxml2/tinyxml2.h" #define IF_NULL_RETURN_FALSE(_x_) if(_x_ == nullptr) return false using namespace tinyxml2 ; USING_NS_CC ; typedef enum { kRoleTypeNone = 0, kRoleTypeBoss,//<曹操 kRoleTypeArmyGeneralHorizontal,//<横向的将军 kRoleTypeArmyGeneralVertical,//<竖直的将军 kRoleTypeSoldier,//<士兵 }RoleType; class Role : public Ref { public: static cocos2d::Vector<Role*> s_roleVec ; static const char * XML_FILE_NAME ; static bool initStatic(); static bool parseData(XMLElement * pElement) ; static void finalizeStatic(); public: Role(); ~Role(); bool init(XMLElement * pElement); RoleType getTypeByChar(const char * pType); private: CC_SYNTHESIZE_READONLY(int ,m_id,ID) ; CC_SYNTHESIZE_READONLY(RoleType,m_type,Type); CC_SYNTHESIZE_READONLY(cocos2d::__String *,m_pImageName,ImageName) ; }; #endif
Role.cpp
#include"Role.h" const char *Role::XML_FILE_NAME = "roles.xml" ; Vector<Role*>Role::s_roleVec ; boolRole::initStatic() { std::stringfilePath = FileUtils::getInstance()->fullPathForFilename(XML_FILE_NAME) ; tinyxml2::XMLDocumentpDoc; FileUtils::getInstance()->setPopupNotify(false); ssize_tfileSize = 0 ; std::stringdata = FileUtils::getInstance()->getStringFromFile(filePath.c_str()); FileUtils::getInstance()->setPopupNotify(true); pDoc.Parse(data.c_str()); XMLElement* pEle = pDoc.RootElement() ; returnparseData(pEle) ; } boolRole::parseData(XMLElement * pElement) { s_roleVec.clear(); XMLElement* child = pElement->FirstChildElement() ; for(;child;child = child->NextSiblingElement()) { if(strcmp(child->Value(),"role") == 0) { Role* pRol = new Role() ; if(!pRol->init(child)) { CC_SAFE_RELEASE_NULL(pRol); returnfalse ; } s_roleVec.pushBack(pRol); pRol->release(); } } returntrue ; } voidRole::finalizeStatic() { s_roleVec.clear(); } Role::Role() :m_id(-1) ,m_pImageName(nullptr) ,m_type(kRoleTypeNone) { } Role::~Role() { CC_SAFE_RELEASE_NULL(m_pImageName); } boolRole::init(XMLElement * pElement) { constchar * pId = pElement->Attribute("id") ; IF_NULL_RETURN_FALSE(pId); m_id= atoi(pId) ; constchar * pImageName = pElement->Attribute("image_name") ; IF_NULL_RETURN_FALSE(pImageName); m_pImageName= new __String(pImageName) ; constchar* pType = pElement->Attribute("type") ; IF_NULL_RETURN_FALSE(pType); m_type= getTypeByChar(pType) ; log("Role:%d",m_id); returntrue; } RoleTypeRole::getTypeByChar(const char * pType) { if(strcmp("boss",pType) == 0) { returnkRoleTypeBoss ; } elseif (strcmp("soldier",pType) == 0) { returnkRoleTypeSoldier ; } elseif (strcmp("general_ver",pType) == 0) { returnkRoleTypeArmyGeneralVertical; } elseif (strcmp("general_hor",pType) == 0) { returnkRoleTypeArmyGeneralHorizontal; } returnkRoleTypeNone ; }
标签:cocos2d-x3.4 游戏 华容道
原文地址:http://blog.csdn.net/c_boy_lu/article/details/45047287