由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。
解决的办法有很多,我这里采用的是用一个XML文档把游戏中需要的中文字都保存起来,这样每次要获取一串中文字符的时候就直接从文档中获取。
为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面
Chinese.h
- #ifndef _CHINESEWORD_H_
- #define _CHINESEWORD_H_
- #include <string>
- #include <cocos2d.h>
- using namespace std;
- using namespace cocos2d;
- static ValueVector txt_vec = FileUtils::getInstance()->getValueVectorFromFile("ChineseWords.xml");
- string ChineseWord(const char* wordId);
- #endif
这里的tex_vec是cocos2dx提供的一个保存文档(xml)内容的一个容器。以后的读取操作就从txt_vec中获取。
这里给出ChineseWords,xml的格式
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <array>
- <dict>
- <key>guanqia</key>
- <string>关卡</string>
- <key>mubiao</key>
- <string>目标</string>
- <key>fen</key>
- <string>分</string>
- <key>highestscore</key>
- <string>历史最高分</string>
- <key>lianji</key>
- <string>连击增加</string>
- <key>shengyu</key>
- <string>剩余</string>
- <key>ge</key>
- <string>个</string>
- <key>jiangli</key>
- <string>奖励</string>
- </dict>
- </array>
- </plist>
再看看ChineseWord的实现
Chinese.cpp
- #include "Chinese.h"
- string ChineseWord(const char* wordId)
- {
- auto txt_map = txt_vec.at(0).asValueMap();
- string ret = txt_map.at(wordId).asString();
- return ret;
- }
就这样,以后在需要用到中文字的地方,就先include这个头文件