通过从xml文件读取中文显示中文需要在Resources文件夹里写一个xml文件,然后通过代码读取xml文件里的中文,具体实现源码如下
HelloWorldScene.h文件
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
CCDictionary *strings; //词典类,加载xml文件用 const char *label_strings;//const char *类型的数据
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp文件
#include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } bool HelloWorld::init() { strings = CCDictionary::createWithContentsOfFile("strings.xml");//读取xml文件 strings->retain();
//获取strings对应的xml文件里key为ch1的内容,得到的是CCString类型的,需要转换成string在转换成const char * label_strings=((CCString*)strings->objectForKey("ch1"))->m_sString.c_str();
CCLabelTTF *ch1=CCLabelTTF::create(label_strings,"",32);//创建内容为label_strings的文本 ch1->setPosition(ccp(240,200)); this->addChild(ch1); label_strings=((CCString*)strings->objectForKey("ch2"))->m_sString.c_str();//获取key为ch2的内容 CCLabelBMFont* ch2=CCLabelBMFont::create(label_strings,"ch.fnt");//通过fnt文件显示特殊的字体 ch2->setPosition(ccp(240,100)); this->addChild(ch2); return true; } strings.xml文件的内容如下
<dict> <key>ch1</key> <string>显示中文</string> <key>ch2</key> <string>特殊字体的中文</string> </dict>
Cocos2dx 2.2.3 win32 源码
http://yunpan.cn/cQS9pIGxr3Jqk (提取码:ac22) (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|