您的位置:首页 > 移动开发 > Cocos引擎

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

2014-03-15 20:36 447 查看
参考: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左右光图片就传了好几次才成功,唔!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-x 乱码 utf-8