Cocos2d-x显示中文与字幕滚动--之游戏开发《赵云要格斗》(14)
最开始在网上找的一个方法,结果在wp8上报错~
在windows环境下使用visual studio 开发cocos2d-x,由于visual studio 默认编码为GBK 格式,而cocos2d-x引擎默认编码为UTF-8, 如果有用到中文,在游戏运行时有可能会出现乱码的情况,这个问题一般有三种解决方案,如下
- 将源码文件保存为utf-8格式(不建议,治标不治本)
- 自己编写编码转换代码,在用到中文的地方手动转换
- 将显示文本单独保存为文本文件(该文件编码为utf-8),由系统统一模块管理文本读取显示,建议使用这种方式,便于后期系统维护,并实现国际化。
第一种方式不介绍了,你懂得,下面对后两种方式简单进行介绍。
一、自己编写编码转换代码,在用到中文的地方手动转换。
---------------
我们程序中设置精灵的坐标时候用到的也应该是这俩个参数,因为我们在实际的写游戏项目的时候都不会写成死的坐标,而是一种相对的位置,比如我们经常通过getWinSize()获得屏幕的分辨率,然后设置精灵的位置在它的几分之几处
2 Ui编辑器中,所用的坐标逗的是%百分比。
写项目的时候都不会写成死的坐标,而是一种相对的位置
--------------------------------------------------------------------------------
键盘值得是?
键盘事件
//Initializing and binding auto listener = EventListenerKeyboard::create(); listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // Implementation of the keyboard event callback function prototype void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { log("Key with keycode %d pressed", keyCode); } void KeyboardTest::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) { log("Key with keycode %d released", keyCode);
----------------------
最开始在网上找的一个方法,结果在wp8上报错~
在windows环境下使用visual studio 开发cocos2d-x,由于visual studio 默认编码为GBK 格式,而cocos2d-x引擎默认编码为UTF-8, 如果有用到中文,在游戏运行时有可能会出现乱码的情况,这个问题一般有三种解决方案,如下
- 将源码文件保存为utf-8格式(不建议,治标不治本)
- 自己编写编码转换代码,在用到中文的地方手动转换
- 将显示文本单独保存为文本文件(该文件编码为utf-8),由系统统一模块管理文本读取显示,建议使用这种方式,便于后期系统维护,并实现国际化。
第一种方式不介绍了,你懂得,下面对后两种方式简单进行介绍。
一、自己编写编码转换代码,在用到中文的地方手动转换。
转换代码如下,可单独保存为 .h头文件,在使用到编码转换的cpp文件include 即可。
#ifdef WIN32
<span class="preproc" style="color: rgb(204, 102, 51);">#define</span> UTEXT(str) GBKToUTF8(str)
<span class="preproc" style="color: rgb(204, 102, 51);">#else</span>
<span class="preproc" style="color: rgb(204, 102, 51);">#define</span> UTEXT(str) str
<span class="preproc" style="color: rgb(204, 102, 51);">#endif</span>
#ifdef WIN32
#include <span class="str" style="color: rgb(0, 96, 128);">"platform/third_party/win32/iconv/iconv.h"</span>
<span class="kwrd" style="color: rgb(0, 0, 255);">static</span> <span class="kwrd" style="color: rgb(0, 0, 255);">char</span> g_GBKConvUTF8Buf[5000] = {0};
<span class="kwrd" style="color: rgb(0, 0, 255);">const</span> <span class="kwrd" style="color: rgb(0, 0, 255);">char</span>* GBKToUTF8(<span class="kwrd" style="color: rgb(0, 0, 255);">const</span> <span class="kwrd" style="color: rgb(0, 0, 255);">char</span> *strChar)
{
iconv_t iconvH;
iconvH = iconv_open(<span class="str" style="color: rgb(0, 96, 128);">"utf-8"</span>,<span class="str" style="color: rgb(0, 96, 128);">"gb2312"</span>);
<span class="kwrd" style="color: rgb(0, 0, 255);">if</span> (iconvH == 0)
{
<span class="kwrd" style="color: rgb(0, 0, 255);">return</span> NULL;
}
size_t strLength = strlen(strChar);
size_t outLength = strLength<<2;
size_t copyLength = outLength;
memset(g_GBKConvUTF8Buf, 0, 5000);
<span class="kwrd" style="color: rgb(0, 0, 255);">char</span>* outbuf = (<span class="kwrd" style="color: rgb(0, 0, 255);">char</span>*) malloc(outLength);
<span class="kwrd" style="color: rgb(0, 0, 255);">char</span>* pBuff = outbuf;
memset( outbuf, 0, outLength);
<span class="kwrd" style="color: rgb(0, 0, 255);">if</span> (-1 == iconv(iconvH, &strChar, &strLength, &outbuf, &outLength))
{
iconv_close(iconvH);
<span class="kwrd" style="color: rgb(0, 0, 255);">return</span> NULL;
}
memcpy(g_GBKConvUTF8Buf,pBuff,copyLength);
free(pBuff);
iconv_close(iconvH);
<span class="kwrd" style="color: rgb(0, 0, 255);">return</span> g_GBKConvUTF8Buf;
}
<span class="preproc" style="color: rgb(204, 102, 51);">#endif</span>
这里,我们自定义了一个宏,简化调用代码风格,这样在使用到中文的地方,我们只需进行如下操作即可:
AppDelegate app;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName(UTEXT(<span class="str" style="color: rgb(0, 96, 128);">"完美世界,完美生活"</span>));
eglView->setFrameSize(900, 600);
<span class="kwrd" style="color: rgb(0, 0, 255);">return</span> CCApplication::sharedApplication()->run();
在 #include "platform/third_party/win32/iconv/iconv.h" 时,有可能会出现系统找不到该头文件的情况,这是因为vs没有导入相关cocos2d-x基本文件导致的,我们手动刚导入确实的文件即可,右键项目,点击属性,如下:
点击后面的下拉箭头选择 edit,在最后一行添加相应类库,
再试试看~
二、将显示文本单独保存为文本文件
在cocos2d-x的示例项目中有关于配置文件读取的示例项目,有兴趣的童鞋可以自己去找下,这里将示例内容简化进行简要介绍。
首先,相关配置文件必须放在项目Resource目录下,可自己设置二级目录,方便管理,
strings.plist内容如下:
<span class="kwrd" style="color: rgb(0, 0, 255);"><?</span><span class="html" style="color: rgb(128, 0, 0);">xml</span> <span class="attr" style="color: rgb(255, 0, 0);">version</span><span class="kwrd" style="color: rgb(0, 0, 255);">="1.0"</span> <span class="attr" style="color: rgb(255, 0, 0);">encoding</span><span class="kwrd" style="color: rgb(0, 0, 255);">="UTF-8"</span>?<span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><!</span><span class="html" style="color: rgb(128, 0, 0);">DOCTYPE</span> <span class="attr" style="color: rgb(255, 0, 0);">plist</span> <span class="attr" style="color: rgb(255, 0, 0);">PUBLIC</span> <span class="kwrd" style="color: rgb(0, 0, 255);">"-//Apple//DTD PLIST 1.0//EN"</span> <span class="kwrd" style="color: rgb(0, 0, 255);">"http://www.apple.com/DTDs/PropertyList-1.0.dtd"</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">plist</span> <span class="attr" style="color: rgb(255, 0, 0);">version</span><span class="kwrd" style="color: rgb(0, 0, 255);">="1.0"</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">dict</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>data<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">dict</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>hello<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">string</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>完美世界,完美生活<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">string</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>cocos2d.x.display_fps<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">true</span><span class="kwrd" style="color: rgb(0, 0, 255);">/></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>cocos2d.x.gl.projection<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">string</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>3d<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">string</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>cocos2d.x.texture.pixel_format_for_png<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">string</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>rgba8888<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">string</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>cocos2d.x.texture.pvrv2_has_alpha_premultiplied<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">false</span><span class="kwrd" style="color: rgb(0, 0, 255);">/></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">dict</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>metadata<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">dict</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>format<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">key</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"><</span><span class="html" style="color: rgb(128, 0, 0);">integer</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>1<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">integer</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">dict</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">dict</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
<span class="kwrd" style="color: rgb(0, 0, 255);"></</span><span class="html" style="color: rgb(128, 0, 0);">plist</span><span class="kwrd" style="color: rgb(0, 0, 255);">></span>
之后我们在自己的cocos2d-x项目导演类实例生成之前需要调用下面代码进行初始化操作:
CCConfiguration::sharedConfiguration()->loadConfigFile(<span class="str" style="color: rgb(0, 96, 128);">"config/strings.plist"</span>);
一般我们在 AppDelegate::applicationDidFinishLaunching() 起始执行该部操作。之后就可以在程序中使用配置文件中的内容了:
CCConfiguration *conf = CCConfiguration::sharedConfiguration();
<span class="kwrd" style="color: rgb(0, 0, 255);">const</span> <span class="kwrd" style="color: rgb(0, 0, 255);">char</span> *helloStr = conf->getCString(<span class="str" style="color: rgb(0, 96, 128);">"hello"</span>, <span class="str" style="color: rgb(0, 96, 128);">"unknown"</span>);
CCLabelTTF* pLabel = CCLabelTTF::create(helloStr, <span class="str" style="color: rgb(0, 96, 128);">"Arial"</span>, 24);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height));
<span class="kwrd" style="color: rgb(0, 0, 255);">this</span>->addChild(pLabel, 1);
这样,就可以将 strings.plist 中的hello项读取出现显示,