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

CCConfiguration::sharedConfiguration()->loadConfigFile cocos2d-x 中文乱码问题及国际化解决方案

2013-08-08 16:32 295 查看
from:://http://www.cnblogs.com/sunguangran/archive/2013/07/29/3222660.html

将显示文本单独保存为文本文件

在cocos2d-x的示例项目中有关于配置文件读取的示例项目,有兴趣的童鞋可以自己去找下,这里将示例内容简化进行简要介绍。

首先,相关配置文件必须放在项目Resource目录下,可自己设置二级目录,方便管理,





strings.plist内容如下:

<?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">
<dict>
<key>data</key>
<dict>
<key>hello</key>
<string>完美世界,完美生活</string>
<key>cocos2d.x.display_fps</key>
<true/>
<key>cocos2d.x.gl.projection</key>
<string>3d</string>
<key>cocos2d.x.texture.pixel_format_for_png</key>
<string>rgba8888</string>
<key>cocos2d.x.texture.pvrv2_has_alpha_premultiplied</key>
<false/>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>1</integer>
</dict>
</dict>
</plist>

之后我们在自己的cocos2d-x项目导演类实例生成之前需要调用下面代码进行初始化操作:

CCConfiguration::sharedConfiguration()->loadConfigFile("config/strings.plist");

一般我们在 AppDelegate::applicationDidFinishLaunching() 起始执行该部操作。之后就可以在程序中使用配置文件中的内容了:

CCConfiguration *conf = CCConfiguration::sharedConfiguration();
const char *helloStr = conf->getCString("hello", "unknown");
CCLabelTTF* pLabel = CCLabelTTF::create(helloStr, "Arial", 24);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height));
this->addChild(pLabel, 1);

这样,就可以将 strings.plist 中的hello项读取出现显示,



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