博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x中中文显示问题解决方法一
阅读量:2304 次
发布时间:2019-05-09

本文共 4710 字,大约阅读时间需要 15 分钟。

原创作品,允许转载,转载时请务必以超链接形式标明文章 、作者信息和本声明。否则将追究法律责任。

 

在VS上使用cocos2d-x开发,中文显示会出问题。下面是解决的方法一(通过xml配置文件来读取中文)

 

------------------------------------------------代码部分----------------------------------------------------------

(1)stringsScript.xml(配置文件,自己写配置文件时就按照下面的格式来,调用时通过userName)

用户名:

 

(2)Singleton.h

#ifndef __SINGLETON_H__#define __SINGLETON_H__template 
class Singleton{public: inline static T* getInstance(); inline static void release();private: static T* t;};template
inline T* Singleton
::getInstance(){ if (!t) { t = new T; } return t;}template
inline void Singleton
::release(){ if (t) { delete t; t = 0; }}template
T* Singleton
::t = 0;#endif // __SINGLE_H__

(3)HXmlParse.h

#ifndef HAnimation_HXmlParse_h#define HAnimation_HXmlParse_h#include "cocos2d.h"using namespace cocos2d;#include using namespace std;class HXmlParse : public CCObject, public CCSAXDelegator{public:	HXmlParse()/*:isJumpHeadData(false)*/{}	static HXmlParse* parserWithFile(const char *tmxFile);	bool initHXmlParse(const char* xmlName);	//  使用 CCSAXDelegator 重写3个回调函数	void startElement(void *ctx, const char *name, const char **atts);	void endElement(void *ctx, const char *name);	void textHandler(void *ctx, const char *ch, int len);	std::string root_name;	//bool isJumpHeadData;	//CCMutableDictionary
*mDic; map
m_mapText;private: std::string startXmlElement;//用来记录每个key前字段 std::string endXmlElement;//用来记录每个key后字段 std::string currString;//记录每个value的值};#endif

(4)HXmlParse.cpp

#include "HXmlParse.h"#include "cocos2d.h"HXmlParse* HXmlParse::parserWithFile(const char *tmxFile){	HXmlParse *pRet = new HXmlParse();	if(pRet->initHXmlParse(tmxFile))	{		//pRet->autorelease();		return pRet;	}	CC_SAFE_DELETE(pRet);	return NULL;}bool HXmlParse::initHXmlParse(const char* xmlName){	//mDic = new CCMutableDictionary
(); CCSAXParser _par; if (false == _par.init("UTF-8") ) { //CCLog("-----请使用utf-8格式!"); return false; } _par.setDelegator(this); const char* _path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(xmlName); return _par.parse(_path);}//回调函数void HXmlParse::startElement(void *ctx, const char *name, const char **atts){ CC_UNUSED_PARAM(ctx); startXmlElement = (char*)name; //if(!isJumpHeadData) { //跳过数据头 //CCLog("------跳过root name"); //isJumpHeadData=true; root_name=startXmlElement; } // CCLog("-startElement----%s",startXmlElement.c_str());}void HXmlParse::endElement(void *ctx, const char *name){ CC_UNUSED_PARAM(ctx); endXmlElement = (char*)name; if(endXmlElement==root_name) { //数据尾 //CCLog("读取xml结束"); //isJumpHeadData=false; root_name=""; } // CCLog("-endElement----%s",endXmlElement.c_str());}//键值对的结束字段void HXmlParse::textHandler(void *ctx, const char *ch, int len){ CC_UNUSED_PARAM(ctx); currString=string((char*)ch,0,len); CCString *ccStr =new CCString();//备注3 ccStr->m_sString=currString; if(root_name!="") { //mDic->setObject(ccStr,startXmlElement); m_mapText[startXmlElement] = ccStr->getCString(); //CCLog("-----key:%s, value:%s",startXmlElement.c_str(),mDic->objectForKey(startXmlElement)->m_sString.c_str()); } // CCLog("-textHandler----%s",currString.c_str());}

(5)XMLConfig.h

#ifndef _XMLCONFIG_H_#define _XMLCONFIG_H_#include "HXmlParse.h"#include "Singleton.h"#include using namespace std;class XMLConfig : public HXmlParse, public Singleton
{public: XMLConfig(); ~XMLConfig(); bool Load(); char* GetStringForKey(const char* sKey) const; bool SetStringForKey(const char* sKey, std::string value);private: HXmlParse* m_pParse;};#endif // _XMLCONFIG_H_

(6)XMLConfig.cpp

#include "XMLConfig.h"XMLConfig::XMLConfig():m_pParse(NULL){}XMLConfig::~XMLConfig(){	m_pParse->release();	m_pParse = NULL;}char* XMLConfig::GetStringForKey(const char* sKey) const{	//const_iterator常量迭代器,不得修改	map
::const_iterator it = m_pParse->m_mapText.find(sKey); if (it != m_pParse->m_mapText.end()) return (char*)it->second.c_str(); return NULL;}bool XMLConfig::SetStringForKey(const char* sKey, std::string value){ map
::iterator it = m_pParse->m_mapText.find(sKey); if ( it != m_pParse->m_mapText.end()) { it->second = value; return true; } else { it->second = ""; return false; }}bool XMLConfig::Load(){ m_pParse = parserWithFile("stringsScript.xml"); return true;}

 

(7)调用部分(包含包含的头文件)

#include "XMLConfig.h"//通过xml配置来读取中文XMLConfig::getInstance()->Load();char * userName=XMLConfig::getInstance()->GetStringForKey("userName");CCLabelTTF * chineseLabel = CCLabelTTF::create(userName,"Thonburi",25);chineseLabel->setAnchorPoint(CCPointZero);chineseLabel->setPosition(ccp(300, 600));this->addChild(chineseLabel,2);
你可能感兴趣的文章
724. Find Pivot Index
查看>>
498. Diagonal Traverse
查看>>
54. Spiral Matrix
查看>>
118. Pascal's Triangle
查看>>
344. Reverse String
查看>>
561. Array Partition I
查看>>
189. Rotate Array
查看>>
283. Move Zeroes
查看>>
80. Remove Duplicates from Sorted Array II
查看>>
41. First Missing Positive
查看>>
299. Bulls and Cows
查看>>
134. Gas Station
查看>>
42. Trapping Rain Water
查看>>
217. Contains Duplicate
查看>>
219. Contains Duplicate II
查看>>
220. Contains Duplicate III
查看>>
TreeSet & TreeMap
查看>>
57. Insert Interval
查看>>
352. Data Stream as Disjoint Intervals
查看>>
239. Sliding Window Maximum
查看>>