键盘事件 中文乱码 适配
Cocos2d-x显示中文与字幕滚动--之游戏开发《赵云要格斗》(14)最开始在网上找的一个方法,结果在wp8上报错~ 在windows环境下使用visual studio 开发cocos2d-x,由于visual studio 默认编码为GBK 格式,而cocos2d-x引擎默认编码为UTF-8, 如果有用到中文,在游戏运行时有可能会出现乱码的情况,这个问题一般有三种解决方案,如下
第一种方式不介绍了,你懂得,下面对后两种方式简单进行介绍。 一、自己编写编码转换代码,在用到中文的地方手动转换。 ---------------
我们程序中设置精灵的坐标时候用到的也应该是这俩个参数,因为我们在实际的写游戏项目的时候都不会写成死的坐标,而是一种相对的位置,比如我们经常通过getWinSize()获得屏幕的分辨率,然后设置精灵的位置在它的几分之几处 -------------------------------------------------------------------------------- 键盘值得是? 键盘事件
除了键盘,设备上的菜单也可以使用这个监听器来处理事件。
上面的触碰事件(原文作者走神,说是鼠标事件),我们用了lambda表达式创建回调函数。我们同时也可以用下面的方法使用CC_CALLBACK_N来绑定已定义的函数:
//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,68)">"Key with keycode %d released", keyCode); ---------------------- Cocos2dx中文乱码问题
分类:Windows phone 8
2014-05-25 20:10
4658人阅读
评论(2)
收藏
举报
cocos2dx
中文乱码
最开始在网上找的一个方法,结果在wp8上报错~ 在windows环境下使用visual studio 开发cocos2d-x,由于visual studio 默认编码为GBK 格式,而cocos2d-x引擎默认编码为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,51);">#else</span> <span class="preproc" style="color: rgb(204,51);">#define</span> UTEXT(str) str <span class="preproc" style="color: rgb(204,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,255);">static</span> <span class="kwrd" style="color: rgb(0,255);">char</span> g_GBKConvUTF8Buf[5000] = {0}; <span class="kwrd" style="color: rgb(0,255);">const</span> <span class="kwrd" style="color: rgb(0,255);">char</span>* GBKToUTF8(<span class="kwrd" style="color: rgb(0,255);">char</span> *strChar) { iconv_t iconvH; iconvH = iconv_open(<span class="str" style="color: rgb(0,128);">"utf-8"</span>,<span class="str" style="color: rgb(0,128);">"gb2312"</span>); <span class="kwrd" style="color: rgb(0,255);">if</span> (iconvH == 0) { <span class="kwrd" style="color: rgb(0,255);">return</span> NULL; } size_t strLength = strlen(strChar); size_t outLength = strLength<<2; size_t copyLength = outLength; memset(g_GBKConvUTF8Buf,5000); <span class="kwrd" style="color: rgb(0,255);">char</span>* outbuf = (<span class="kwrd" style="color: rgb(0,255);">char</span>*) malloc(outLength); <span class="kwrd" style="color: rgb(0,255);">char</span>* pBuff = outbuf; memset( outbuf,outLength); <span class="kwrd" style="color: rgb(0,255);">if</span> (-1 == iconv(iconvH,&strChar,&strLength,&outbuf,&outLength)) { iconv_close(iconvH); <span class="kwrd" style="color: rgb(0,255);">return</span> NULL; } memcpy(g_GBKConvUTF8Buf,pBuff,copyLength); free(pBuff); iconv_close(iconvH); <span class="kwrd" style="color: rgb(0,255);">return</span> g_GBKConvUTF8Buf; } <span class="preproc" style="color: rgb(204,51);">#endif</span> 这里,我们自定义了一个宏,简化调用代码风格,这样在使用到中文的地方,我们只需进行如下操作即可: AppDelegate app; CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setViewName(UTEXT(<span class="str" style="color: rgb(0,128);">"完美世界,完美生活"</span>)); eglView->setFrameSize(900,600); <span class="kwrd" style="color: rgb(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,255);"><?</span><span class="html" style="color: rgb(128,0);">xml</span> <span class="attr" style="color: rgb(255,0);">version</span><span class="kwrd" style="color: rgb(0,255);">="1.0"</span> <span class="attr" style="color: rgb(255,0);">encoding</span><span class="kwrd" style="color: rgb(0,255);">="UTF-8"</span>?<span class="kwrd" style="color: rgb(0,255);">></span> <span class="kwrd" style="color: rgb(0,255);"><!</span><span class="html" style="color: rgb(128,0);">DOCTYPE</span> <span class="attr" style="color: rgb(255,0);">plist</span> <span class="attr" style="color: rgb(255,0);">PUBLIC</span> <span class="kwrd" style="color: rgb(0,255);">"-//Apple//DTD PLIST 1.0//EN"</span> <span class="kwrd" style="color: rgb(0,255);">"http://www.apple.com/DTDs/PropertyList-1.0.dtd"</span><span class="kwrd" style="color: rgb(0,255);"><</span><span class="html" style="color: rgb(128,255);">="1.0"</span><span class="kwrd" style="color: rgb(0,0);">dict</span><span class="kwrd" style="color: rgb(0,255);">></span> <span class="kwrd" style="color: rgb(0,0);">key</span><span class="kwrd" style="color: rgb(0,255);">></span>data<span class="kwrd" style="color: rgb(0,255);"></</span><span class="html" style="color: rgb(128,255);">></span> <span class="kwrd" style="color: rgb(0,255);">></span>hello<span class="kwrd" style="color: rgb(0,0);">string</span><span class="kwrd" style="color: rgb(0,255);">></span>完美世界,完美生活<span class="kwrd" style="color: rgb(0,255);">></span>cocos2d.x.display_fps<span class="kwrd" style="color: rgb(0,0);">true</span><span class="kwrd" style="color: rgb(0,255);">/></span> <span class="kwrd" style="color: rgb(0,255);">></span>cocos2d.x.gl.projection<span class="kwrd" style="color: rgb(0,255);">></span>3d<span class="kwrd" style="color: rgb(0,255);">></span>cocos2d.x.texture.pixel_format_for_png<span class="kwrd" style="color: rgb(0,255);">></span>rgba8888<span class="kwrd" style="color: rgb(0,255);">></span>cocos2d.x.texture.pvrv2_has_alpha_premultiplied<span class="kwrd" style="color: rgb(0,0);">false</span><span class="kwrd" style="color: rgb(0,255);">/></span> <span class="kwrd" style="color: rgb(0,255);">></span>metadata<span class="kwrd" style="color: rgb(0,255);">></span>format<span class="kwrd" style="color: rgb(0,0);">integer</span><span class="kwrd" style="color: rgb(0,255);">></span>1<span class="kwrd" style="color: rgb(0,0);">plist</span><span class="kwrd" style="color: rgb(0,255);">></span> 之后我们在自己的cocos2d-x项目导演类实例生成之前需要调用下面代码进行初始化操作: CCConfiguration::sharedConfiguration()->loadConfigFile(<span class="str" style="color: rgb(0,128);">"config/strings.plist"</span>); 一般我们在 AppDelegate::applicationDidFinishLaunching() 起始执行该部操作。之后就可以在程序中使用配置文件中的内容了: CCConfiguration *conf = CCConfiguration::sharedConfiguration(); <span class="kwrd" style="color: rgb(0,255);">char</span> *helloStr = conf->getCString(<span class="str" style="color: rgb(0,128);">"hello"</span>,128);">"unknown"</span>); CCLabelTTF* pLabel = CCLabelTTF::create(helloStr,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,255);">this</span>->addChild(pLabel,1); 这样,就可以将 strings.plist 中的hello项读取出现显示,
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |