cocos2dx显示中文

来源:互联网 发布:知乎为什么那么火 编辑:程序博客网 时间:2024/05/21 21:38

从外部文件读取UTF-8

推荐大家使用资源文件进行配置保存,如xml将其采用的UTF-8的编码方式保存,自然会让我想到,日文、韩文等待各种国家的语言都可以正常显示了,为了你的软件国际化...尽量采用这种方式吧!到时候根据手机系统的语言,然后动态的来读取你文件中的资源...

先看下我们的xml文件:

[html] view plaincopy
  1. <dict>  
  2.     <key>chinese1</key>  
  3.     <string>美好的一天</string>  
  4.     <key>japanese</key>  
  5.     <string>良い一日を</string>  
  6.     <key>spanish</key>  
  7.     <string>Buen día</string>  
  8. </dict>  
  9. </plist>  

然后再来看如何使用:

[cpp] view plaincopy
  1. //利用CCDictionary来读取xml  
  2.     CCDictionary *strings = CCDictionary::create("fonts/strings.xml");  
  3.     //中文,日语,西班牙语:objectForKey根据key,获取对应的string  
  4.     const char *chinese = ((CCString*)strings->objectForKey("chinese1"))->m_sString.c_str();  
  5.     const char *japanese = ((CCString*)strings->objectForKey("japanese"))->m_sString.c_str();  
  6.     const char *spanish = ((CCString*)strings->objectForKey("spanish"))->m_sString.c_str();  
  7.   
  8.     CCLabelBMFont *label1 = CCLabelBMFont::create(spanish, "fonts/arial-unicode-26.fnt");  
  9.     addChild(label1);  
  10.     label1->setPosition(ccp(s.width/2, s.height/4*3-20));  
  11.   
  12.     CCLabelBMFont *label2 = CCLabelBMFont::create(chinese, "fonts/arial-unicode-26.fnt");  
  13.     addChild(label2);  
  14.     label2->setPosition(ccp(s.width/2, s.height/4*2));  
  15.   
  16.     CCLabelBMFont *label3 = CCLabelBMFont::create(japanese, "fonts/arial-unicode-26.fnt");  
  17.     addChild(label3);  
  18.     label3->setPosition(ccp(s.width/2, s.height/4*1));  

运行效果:

0 0