cocos2d-x 读写 xml 文件

前端之家收集整理的这篇文章主要介绍了cocos2d-x 读写 xml 文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x 读写 xml 文件



A product of cheungmine

使用cocos2d-x开发2d游戏确实方便,但是对于一般的小游戏,经常需要的工作是UI布局设计和调整,代码改来改去,真不方便。现成的Cocos Studio或者SpriteBuilder当然更适合设计游戏。作为程序员,如果想在纯命令行模式下开发游戏,早晚要有自己的UI模块。不妨称之为cocos2d-layout。cocos2d-layout相当于舞台的布景。布景师根据导演的要求(xml)来生成舞台。布景师在现实生活中当然是人来做,在程序里就是一段程序或代码库。这个没用通用的万能的库可以做这个事情,因为游戏的内容是差别很大的。因此一个好的游戏开发师的工具箱里肯定有这样一个瑞士军刀。敝人刚入门,就从最简单的代码开始。

使用xml来做这样的配置是最适合不过的。cocos2d-x提供了读写xml的C++库tinyxml2(https://github.com/leethomason/tinyxml2),当然是开源的,使用起来相当简单:

#include "tinyxml2/tinyxml2.h"

using namespace tinyxml2;

在你的cocos2d代码中放入下面的代码调用之:

  1. static void makeXml(const char *fileName)
  2. {
  3. std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
  4.  
  5. tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
  6.  
  7. //xml 声明(参数可选)
  8. XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
  9. pDoc->LinkEndChild(pDel);
  10. //添加plist节点
  11. XMLElement *plistElement = pDoc->NewElement("plist");
  12. plistElement->SetAttribute("version","1.0");
  13. pDoc->LinkEndChild(plistElement);
  14. XMLComment *commentElement = pDoc->NewComment("this is xml comment");
  15. plistElement->LinkEndChild(commentElement);
  16. //添加dic节点
  17. XMLElement *dicElement = pDoc->NewElement("dic");
  18. plistElement->LinkEndChild(dicElement);
  19. //添加key节点
  20. XMLElement *keyElement = pDoc->NewElement("key");
  21. keyElement->LinkEndChild(pDoc->NewText("Text"));
  22. dicElement->LinkEndChild(keyElement);
  23. XMLElement *arrayElement = pDoc->NewElement("array");
  24. dicElement->LinkEndChild(arrayElement);
  25. for (int i = 0; i<3; i++) {
  26. XMLElement *elm = pDoc->NewElement("name");
  27. elm->LinkEndChild(pDoc->NewText("Cocos2d-x"));
  28. arrayElement->LinkEndChild(elm);
  29. }
  30. pDoc->SaveFile(filePath.c_str());
  31. pDoc->Print();
  32. delete pDoc;
  33. }
  34.  
  35. static void parseXML(const char *fileName)
  36. {
  37. std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
  38. tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
  39. XMLError errorId = pDoc->LoadFile(filePath.c_str());
  40. if (errorId != 0) {
  41. //xml格式错误
  42. return;
  43. }
  44. XMLElement *rootEle = pDoc->RootElement();
  45. //获取第一个节点属性
  46. const XMLAttribute *attribute = rootEle->FirstAttribute();
  47. //打印节点属性名和值
  48. log("attribute<em>name = %s,attribute</em>value = %s",attribute->Name(),attribute->Value());
  49. XMLElement *dicEle = rootEle->FirstChildElement("dic");
  50. XMLElement *keyEle = dicEle->FirstChildElement("key");
  51. if (keyEle) {
  52. log("keyEle Text= %s",keyEle->GetText());
  53. }
  54. XMLElement *arrayEle = keyEle->NextSiblingElement();
  55. XMLElement *childEle = arrayEle->FirstChildElement();
  56. while ( childEle ) {
  57. log("childEle Text= %s",childEle->GetText());
  58. childEle = childEle->NextSiblingElement();
  59. }
  60. delete pDoc;
  61. }

当然上面的代码是网上搜来的。只是一个例子。如果想用C来解析xml,有很多选择:libxml,expat,ezxml等。我发现一个很优秀的库:mini-xml。 http://www.msweet.org/projects.php?Z3
  1. Mini-XML is a small XML library that you can use to read and write XML
  2. and XML-like data files in your application without requiring large
  3. non-standard libraries.
  4.  
  5. Mini-XML only requires an ANSI C compatible compiler (GCC works,as do
  6. most vendors' ANSI C compilers) and a 'make' program.
  7.  
  8. Mini-XML supports reading of UTF-8 and UTF-16 and writing of UTF-8
  9. encoded XML files and strings. Data is stored in a linked-list tree
  10. structure,preserving the XML data hierarchy,and arbitrary element
  11. names,attributes,and attribute values are supported with no
  12. preset limits,just available memory.

那么我准备用这个来做cocos2d-layout。

猜你在找的Cocos2d-x相关文章