cocos2dx中文乱码解决办法

将要在游戏中用到的文字写到xml文件中,如strings.xml,以UTF-8格式保存

<?xml version="1.0" encoding="UTF-8"?>
<dict>
    <key>title</key> 
    <string>跑酷</string> 
    <key>start</key> 
    <string>开始</string> 
	<key>exit</key> 
    <string>退出</string> 
</dict> 

 

然后创建工具类StringReader

StringReader.h

#ifndef __STRINGREADER_H__
#define __STRINGREADER_H__
#include "cocos2d.h"
USING_NS_CC;
class StringReader
{
public:
	~StringReader();
	static StringReader* getInstance();
	const char* getValue(const std::string& key);
private:
	static StringReader* _instance;
	static CCDictionary* map;
};

#endif // !__STRINGREADER_H__

 

StringReader.cpp

#include "StringReader.h"

StringReader* StringReader::_instance = nullptr;
CCDictionary* StringReader::map = nullptr;
StringReader* StringReader::getInstance(){
	if (_instance == nullptr){
		_instance = new StringReader();
		map = CCDictionary::createWithContentsOfFile("strings.xml");
	}
	return _instance;
}

const char* StringReader::getValue(const std::string& key){
	return ((CCString*)map->objectForKey(key))->getCString();
}

StringReader::~StringReader(){
	if (_instance != nullptr){
		delete _instance;
		delete map;
		_instance = nullptr;
		map = nullptr;
	}
}

 用法:

auto strings = StringReader::getInstance();
	const char* title = strings->getValue("title");
	const char* start = strings->getValue("start");
	const char* exit = strings->getValue("exit");

 

你可能感兴趣的:(cocos2dx中文乱码解决办法)