cocos2d-x解决中文乱码问题(详细)!(跨平台)

来源:互联网 发布:c4d r18 mac 破解版 编辑:程序博客网 时间:2024/06/13 23:08

参考:http://www.2cto.com/kf/201305/215140.html

出现乱码原因:.cpp文件默认编码GBK

解决办法将字符串单独放到一个文件中:我放在了资源文件夹下strings.xml文件中。

内容如下:

<dict>
     <key>Hello</key>
     <string>你好!</string>
     <key>Info</key>
     <string>我是hahaya。</string>
</dict>

注意:保存文件的时候用UTF-8编码,

 

 

 

 

 

然后建立一个工具类:

Str_covertCode.h :

#include "cocos2d.h"USING_NS_CC;class Str_convertCode{public:static const char*  Str_convertCodeByKey(char* str){CCDictionary  *itemsString=CCDictionary::createWithContentsOfFile("strings.xml");const char* itemString =((CCString*)itemsString->objectForKey(str))->m_sString.c_str();return itemString;}};/** 在cocos2d-x中使用中文 **/ //利用CCDictionary来读取xml  //CCDictionary *strings = CCDictionary::createWithContentsOfFile("strings.xml"); // strings.xml 文件 在资源目录下//读取Hello键中的值 objectForKey根据key,获取对应的string  //const char *hello = ((CCString*)strings->objectForKey("Hello"))->m_sString.c_str(); 

使用如下:

CCMenuItemFont::create(Str_convertCode::Str_convertCodeByKey("Hello"));

只需要一个参数,就是上面strings.xml文件中的KEY 字段。

 

 

第一次写博客加上网速在10kb左右光图片就传了好几次才成功,唔!

 

0 0
原创粉丝点击