实现 1 新建XML文件,按照一定的格式,输入相应要显示的中文,最后一定需要使用UTF-8格式保存文件
[html] <dict> <key>Hello</key> <string>你好!</string> <key>Info</key> <string>我是hahaya。</string> </dict>
2 代码实现
[cpp] /** 在cocos2d-x中使用中文 **/ //利用CCDictionary来读取xml
CCDictionary *strings = CCDictionary::createWithContentsOfFile("strings.xml"); //读取Hello键中的值 objectForKey根据key,获取对应的string
const char hello = ((CCString)strings->objectForKey("Hello"))->m_sString.c_str(); //读取Info键中的值
const char info = ((CCString)strings->objectForKey("Info"))->m_sString.c_str();
//显示
CCLabelTTF *labelHello = CCLabelTTF::create(hello, "Arial", 24); labelHello->setPosition( ccp(size.width / 2, size.height - 50) ); this->addChild(labelHello, 1);
CCLabelTTF *labelInfo = CCLabelTTF::create(info, "Arial", 30); labelInfo->setPosition( ccp(size.width / 2, size.height - 100) ); this->addChild(labelInfo, 1);
/** 在cocos2d-x中使用中文 **/ //利用CCDictionary来读取xml CCDictionary *strings = CCDictionary::createWithContentsOfFile("strings.xml"); //读取Hello键中的值 objectForKey根据key,获取对应的string const char hello = ((CCString)strings->objectForKey("Hello"))->m_sString.c_str(); //读取Info键中的值 const char info = ((CCString)strings->objectForKey("Info"))->m_sString.c_str();
//显示 CCLabelTTF *labelHello = CCLabelTTF::create(hello, "Arial", 24); labelHello->setPosition( ccp(size.width / 2, size.height - 50) ); this->addChild(labelHello, 1);
CCLabelTTF *labelInfo = CCLabelTTF::create(info, "Arial", 30); labelInfo->setPosition( ccp(size.width / 2, size.height - 100) ); this->addChild(labelInfo, 1);