cocos2d-x显示中文,方法(1)

       我们用两种方法来实现在cocos2d-x中显示中文,在这之前,我会认为你已经按照子龙山人的 文章配置了环境,如若不了解的请先移步我转载的子龙山人前辈的文章Cocos2d-x win7 + vs2010 配置图文详解(亲测)Cocos2d-x建工程时避免copy文件夹和库。我们新建一个工程,取名为LabelTest1

       首先我们来看一下第一种方法,即TestCpp示例使用的方法。

       第一步我们新建一个XML文件“strings.xml”,并确保文件存放在工程目录的Resources文件夹下,否则会提示找不到xml文件。同时找到TestCpp示例里面的字体文件同样复制到Resources文件夹下,这里我把D:\cocos2d-2.1beta3-x-2.1.1\cocos2d-2.1beta3-x-2.1.1\samples\Cpp\TestCpp\Resources\fonts(你的可能不完全一致)下面的字体文件arial-unicode-26.fnt和arial-unicode-26.png复制到了我们新建的工程中并添加进工程。最终Resources文件夹如下:

                    

然后我们修改strings.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<plist version="1.0">
  <dict>
    <key>chinese</key>
    <string>美好</string>
  </dict>
</plist>

接下来就是在代码中使用了,打开我们的HelloWorld.cpp文件,修改其中的init()函数:

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //
        // super init first
        //

        CC_BREAK_IF(! CCLayer::init());

        //
        // add your codes below...
        //

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);
      
        CCSize size = CCDirector::sharedDirector()->getWinSize();

	//使用创建的xml文件,取得定义的汉字
	CCDictionary *strings = CCDictionary::createWithContentsOfFile("strings.xml");
        const char *chinese = ((CCString*)strings->objectForKey("chinese"))->m_sString.c_str();

	//将文字作为标题显示出来,并设置字体和大小
	CCLabelBMFont *label = CCLabelBMFont::create(chinese,"arial-unicode-26.fnt");
	label->setPosition(ccp(size.width/2, size.height*0.7f));
        this->addChild(label,2);
    
        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

        bRet = true;
    } while (0);

    return bRet;
}

运行,效果如下:

                       

你会发现只能显示“美”,“好”,“的”,“日”,等几个固定的汉字,其他的不会显示出来。这是因为咱们使用的TestCpp里面的自定义字体只是定义了这几个汉字而已,并未对其他更多的汉字进行定义,所以不会显示。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值