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

通过从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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值