方法:将字符串保存在 XML 文件中,在 Cocos2d-x 中使用 Dictionary 读出字符串
string.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>gameName</key><string>游戏名称</string>
<key>copyright</key><string>Copyright © MWN </string>
</dict>
</plist>
使用方法:
#include "LogoScene.h"
USING_NS_CC;
Scene* LogoScene::createScene()
{
auto scene = Scene::create();
auto layer = LogoScene::create();
scene->addChild( layer );
return scene;
}
bool LogoScene::init()
{
if (!LayerColor::initWithColor( Color4B::BLACK ) )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
// 从 XML 文件读字符串
Dictionary * dic = Dictionary::createWithContentsOfFile("string.xml");
const char * STR_GAME_NAME = ((String*)dic->objectForKey("gameName"))->getCString();
const char * STR_COPYRIGHT = ((String*)dic->objectForKey("copyright"))->getCString();
// 游戏名称
auto gameName = Label::createWithSystemFont( STR_GAME_NAME , "", 40);
this->addChild( gameName );
gameName->setColor( Color3B::WHITE );
gameName->setPosition( Vec2( visibleSize.width / 2, visibleSize.height / 2 + visibleSize.height / 4 ) );
// 版权信息
auto copyright = Label::createWithSystemFont( STR_COPYRIGHT, "", 20);
this->addChild(copyright);
copyright->setColor(Color3B::WHITE);
copyright->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 4));
return true;
}
运行效果图