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

Cocos2d-x 中文显示的方法

2016-05-12 16:27 501 查看
在Cocos2d-x中显示中文的方法很多种,其根本原因是我们程序文件的编码问题,也就是UTF-8编码的问题,用最简单的方法就是把你的程序文本通过另存为UTF-8编码即可,但除了这种方法还有其他的很多方法,下面介绍下用过的集中方法:

第一种方法就是通过程序代码实现改变UTF8编码来显示中文

如下代码:

//参数传递中文即可
char* RunAction::FontToUTF8(const char* font)
{
int len = MultiByteToWideChar(CP_ACP, 0, font, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, font, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr)delete[] wstr;
return str;
}
也就是在你的文件添加FontToUTF8这个函数即可,通过函数我们知道它返回的是char*类型,使用:FontToUTF8("中文")。

第二种法是通过读取xml文件显示中文,这里我们要分三步来做

①创建一个XML文件,格式如下(注:记得要以UTF-8编码保存)

<dict>
<key>label</key>
<string>中文</string>
<key>name</key>
<string>洪爷工作室</string>
<key>show</key>
<string>在Cocos2d-x中显示中文</string>
</dict>
②通过词典类(Dictionary)读取xml文件(注:xml文件路径默认为我们工程文件reSource文件夹)

Dictionary* readXMLFile = Dictionary::createWithContentsOfFile("ZZChinese.xml");


在引擎开发中,通过名称或索引快速找到所对应的节点,资源,物体是最最基础的算法需求。如何更高效的满足引擎灵活的存储与查询需求,是任何一款引擎要考虑的问题,在诸多款开源或商业引擎中,都有过为了解决此类需求而专门设计的模版类或容器,它们通常被称为“词典”或“字典”。

我们在Cocos2d-x中的很多地方也遇到过词典类CCDictionary。它是利用哈希表算法来进行CCObject管理的一个类。学习它可以让我们对于Cocos2d-x的内部管理机制有更清晰的认识。同时也可以学习这种思想,帮助我们在以后的项目开发中设计出更棒的管理器架构
通过词典类的键值(key)来获取值(value)

//通过键值(key)和取值(value)读取字符串
const char* str = ((String*)readXMLFile->objectForKey("language"))->getCString();
③引用读取的中文

//创建文本
auto label = Label::create(str, "Arial", 48));
label->setposition(200,100);
label->setColor(Color3B(188,0,188));
this.addChild(label);


最后上代码:

在init()函数添加如下代码
<pre name="code" class="html">bool RunAction::init()
{
if ( !Layer::init() )
{
return false;
}
//通过词典类读取文件
Dictionary* readFile = Dictionary::createWithContentsOfFile("ZZChinese.xml");
//通过键值(key)和取值(value)读取字符串
const char* str = ((String*)readFile->objectForKey("label"))->getCString();
const char* str1 = ((String*)readFile->objectForKey("name"))->getCString();
const char* str2 = ((String*)readFile->objectForKey("show"))->getCString();

//开关按钮
auto toggleItem = MenuItemToggle::createWithCallback(
CC_CALLBACK_1(RunAction::menuMusicToggleCallback,this),
MenuItemLabel::create(Label::create(str, "Arial", 48)),
MenuItemLabel::create(Label::create(FontToUTF8("英语"), "微软雅黑", 48)),
MenuItemLabel::create(Label::create(str1, "Marker Felt", 48)),
MenuItemLabel::create(Label::create(str2, "Arial", 48)),
NULL);
toggleItem->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
toggleItem->setColor(Color3B(108, 10, 200));
auto menu = Menu::create(toggleItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu);
return true;
}



char* RunAction::FontToUTF8(const char* font)
{
int len = MultiByteToWideChar(CP_ACP, 0, font, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, font, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr)delete[] wstr;
return str;
}
效果图:

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