Cocos2dx------通过从xml文件读取中文显示中文,通过fnt文件显示特殊的字体

来源:互联网 发布:淘宝怎么看进店关键词 编辑:程序博客网 时间:2024/06/01 09:10

通过从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)




0 0
原创粉丝点击