利用cocos2dx 3.2开发消灭星星(六)如何在cocos2dx中显示中文

由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。
解决的办法有很多,我这里采用的是用一个XML文档把游戏中需要的中文字都保存起来,这样每次要获取一串中文字符的时候就直接从文档中获取。
为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面
Chinese.h

#ifndef _CHINESEWORD_H_
#define _CHINESEWORD_H_
#include 
#include 
using namespace std;
using namespace cocos2d;

static ValueVector txt_vec = FileUtils::getInstance()->getValueVectorFromFile("ChineseWords.xml");

string ChineseWord(const char* wordId);

#endif

```

这里的tex_vec是cocos2dx提供的一个保存文档(xml)内容的一个容器。以后的读取操作就从txt_vec中获取。

这里给出ChineseWords,xml的格式
<?xml version="1.0" encoding="UTF-8"?>



  
    guanqia
    关卡
    
  mubiao
  目标
  
  fen
  分
  
  highestScore
  历史最高分
  
  lianji
  连击增加
  
  shengyu
  剩余
  
  ge
  个
  
  jiangli
  奖励





```

再看看ChineseWord的实现
Chinese.cpp
#include "Chinese.h"
string ChineseWord(const char* wordId)
{
    auto txt_map = txt_vec.at(0).asValueMap();
    string ret = txt_map.at(wordId).asString();
    return ret;
}

```

就这样,以后在需要用到中文字的地方,就先include这个头文件
然后调用ChineseWord函数,获取一串中文字符串。

为什么 转到安卓项目的时候 提示 cocos2d: fullPathForFilename: No file found at chinesewords.xml. Possible missing file.

你把XML另存为 ansi

xml文件放在哪里?Resources吗?

楼主,音乐资源有MP3格式的么?求音乐资源,谢谢!