现在的位置: 首页 > 综合 > 正文

cocos2d-x3.0从XML读取中文

2013年12月10日 ⁄ 综合 ⁄ 共 852字 ⁄ 字号 评论关闭

网上很多这样的文档,3.0只不过是有些变动。

直接上代码:

保存的XML文件放到resources文件夹里,具体格式自己定,但要是uft8的。比如:

<?xml version="1.0" encoding="utf-8"?>
<dict>
    <key>KeyWordsFind</key>
    <string>关键字查找</string>
    <key>SuarAyaFind</key>
    <string>按章节查找</string>
</dict>

解析代码也很少:

	CCDictionary *strings = CCDictionary::createWithContentsOfFile("word.xml");
	//读取Hello键中的值 objectForKey根据key,获取对应的string
	const char *keyWordsFind = ((String*)strings->objectForKey("KeyWordsFind"))->getCString();
	//读取Info键中的值
	const char *suarAyaFind = ((CCString*)strings->objectForKey("SuarAyaFind"))->getCString();

	//显示
	auto labelHello = LabelTTF::create(keyWordsFind, "Arial", 24);
	labelHello->setPosition( ccp(visibleSize.width-100 , visibleSize.height - 50) );
	this->addChild(labelHello, 1);

	auto labelInfo = LabelTTF::create(suarAyaFind, "Arial", 24);
	labelInfo->setPosition( ccp(visibleSize.width-100, visibleSize.height - 100) );
	this->addChild(labelInfo, 1);

效果如下:

是不是很easy哇。

抱歉!评论已关闭.