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

from:://http://www.cnblogs.com/sunguangran/archive/2013/07/29/3222660.html

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

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

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

image

    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项读取出现显示,

image

 
 
分类: cocos2d-x
原文地址:https://www.cnblogs.com/wanqieddy/p/3246117.html