这里写一个工具类——XMLParser,用来解决游戏中出现的中文乱码问题。
主要是把游戏中要使用到的文字存储在XML文件中,通过加载这个文件,并且访问name节点,来获取相对应的中文。
XMLParser.h如下
// // XMLParse.h // Game1 // // Created by imac on 15-7-24. // // #ifndef __Game1__XMLParse__ #define __Game1__XMLParse__ #include "cocos2d.h" USING_NS_CC; using namespace std; class XMLParser : public cocos2d::Ref,public cocos2d::SAXDelegator { public: // 解析指定的xml文件 static XMLParser* parseWithFile(const char *xmlFileName); static XMLParser* parseWithString(const char *content); XMLParser(); virtual ~XMLParser(); // 从本地xml文件读取 bool initWithFile(const char *xmlFileName); // 从字符中读取,可用于读取网络中的xml数据 bool initWithString(const char *content); // 对应xml标签开始,如:<string name="app_name"> virtual void startElement(void *ctx,const char *name,const char **atts); // 对应xml标签结束,如:</string> virtual void endElement(void *ctx,const char *name); // 对应xml标签文本 virtual void textHandler(void *ctx,const char *s,int len); // 获取对应标签的字符串 cocos2d::String* getString(const char *key); private: cocos2d::CCDictionary *m_pDictionary; std::string m_key; std::string startXMLElement; std::string endXMLElement; }; #endif /* defined(__Game1__XMLParse__) */
XMLParser.cpp如下
// // XMLParse.cpp // Game1 // // Created by imac on 15-7-24. // // #include "XMLParse.h" //字符ascii码 // 空格 const static int SPACE = 32; // 换行 const static int NEXTLINE = 10; // tab 横向制表符 const static int TAB = 9; string replace(string source,string pattern,string dstPattern) { string result; string::size_type pos; int lenSource = source.length(); int i = 0; for (i = 0; i < lenSource; ++i) { pos = source.find(pattern,i); if (string::npos == pos) break; if (pos < lenSource) { string s = source.substr(i,pos - i); result += s; result += dstPattern; i = pos + pattern.length() - 1; } } result += source.substr(i); return result; } XMLParser* XMLParser::parseWithFile(const char *xmlFileName) { XMLParser *pXMLParser = new XMLParser(); if (pXMLParser->initWithFile(xmlFileName)) { pXMLParser->autorelease(); return pXMLParser; } CC_SAFE_DELETE(pXMLParser); return NULL; } bool XMLParser::initWithFile(const char *xmlFileName) { m_pDictionary = new CCDictionary(); SAXParser _parser; _parser.setDelegator(this); //获取文件全路径 string fullPath = FileUtils::getInstance()->fullPathForFilename(xmlFileName); CCLog("xml parser full path : %s",fullPath.c_str()); return _parser.parse(fullPath); } XMLParser* XMLParser::parseWithString(const char *content) { XMLParser *pXMLParser = new XMLParser(); if (pXMLParser->initWithString(content)) { pXMLParser->autorelease(); return pXMLParser; } CC_SAFE_DELETE(pXMLParser); return NULL; } bool XMLParser::initWithString(const char *content) { m_pDictionary = new CCDictionary(); SAXParser _parse; _parse.setDelegator(this); return _parse.parse(content,strlen(content)); } //开始一个节点 // 比如 <string name="muzhuang">木n桩n怪</string> //name 为 :string //atts[0] 为属性 : name //atts[1] 为值 : app_name //atts[2] 以此类推 void XMLParser::startElement(void *ctx,const char **atts) { this->startXMLElement = (char *)name; CCLog("start=%s",startXMLElement.c_str());//name if (this->startXMLElement == "string") { while (atts && *atts) { CCLog("attrs0=%s",atts[0]); //atts[0] : name CCLog("attrs1=%s",atts[1]); //atts[1] : app_name const char *attsKey = *atts; if (0 == strcmp(attsKey,"name")) { ++atts; const char *attsValue = *atts; m_key = attsValue; //key break; } ++atts; } } } void XMLParser::endElement(void *ctx,const char *name) { this->endXMLElement = (char *)name; CCLog("end=%s",endXMLElement.c_str()); } void XMLParser::textHandler(void *ctx,int len) { string value((char *)s,len); //是否全是非正常字符 bool noValue = true; for (int i = 0; i < len; ++i) { if (s[i] != SPACE && s[i] != NEXTLINE && s[i] != TAB) { noValue = false; break; } } if (noValue) return; string result = replace(value,string("n"),string("n")); CCString *pString = CCString::create(result); CCLog("key=%s value=%s",m_key.c_str(),pString->getCString()); this->m_pDictionary->setObject(pString,this->m_key); } String* XMLParser::getString(const char *key) { string strKey(key); return (String *)this->m_pDictionary->objectForKey(strKey); } XMLParser::XMLParser() { } XMLParser::~XMLParser() { CC_SAFE_DELETE(this->m_pDictionary); }
XMLParser的使用:
XMLParser *pXmlParser = XMLParser::parseWithFile("tujiLayer.xml"); String *mz = pXmlParser->getString("muzhuang"); m_pMZLabel = LabelTTF::create(mz->getCString(),"",30); // 注意此处的getCString()
这样一来,中文乱码问题就得到解决了。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|