键盘事件 中文乱码 适配

Cocos2d-x显示中文与字幕滚动--之游戏开发《赵云要格斗》(14)


最开始在网上找的一个方法,结果在wp8上报错~

在windows环境下使用visual studio 开发cocos2d-x,由于visual studio 默认编码为GBK 格式,而cocos2d-x引擎默认编码为UTF-8, 如果有用到中文,在游戏运行时有可能会出现乱码的情况,这个问题一般有三种解决方案,如下

  • 将源码文件保存为utf-8格式(不建议,治标不治本)
  • 自己编写编码转换代码,在用到中文的地方手动转换
  • 将显示文本单独保存为文本文件(该文件编码为utf-8),由系统统一模块管理文本读取显示,建议使用这种方式,便于后期系统维护,并实现国际化。

    第一种方式不介绍了,你懂得,下面对后两种方式简单进行介绍。

一、自己编写编码转换代码,在用到中文的地方手动转换。

---------------

2、五种适配模式

    从上面的讲解我们可以了解到,setFrameSize()是设置了窗口大小(即屏幕的实际大小),而这个参数只是为了我们开发时作为模拟参照,在实际手机上运行时,手机的屏幕大小是我们无法设置的。

    而屏幕适配的关键在于setDesignResolutionSize(),通过它来设置可视区域的分辨率以及屏幕适配模式。该函数的前两个参数为分辨率(即屏幕长宽比例),而最后一个参数则是适配的模式。


    2.1、适配模式

    (1)ResolutionPolicy::EXACT_FIT    :拉伸变形,使铺满屏幕。

    (2)ResolutionPolicy::NO_BORDER    :按比例放缩,全屏展示不留黑边。

                                             (长宽中小的铺满屏幕,大的超出屏幕)

    (3)ResolutionPolicy::SHOW_ALL     :按比例放缩,全部展示不裁剪。

                                             (长宽中大的铺满屏幕,小的留有黑边)

    (4)ResolutionPolicy::FIXED_WIDTH  :按比例放缩,宽度铺满屏幕。

    (5)ResolutionPolicy::FIXED_HEIGHT :按比例放缩,高度铺满屏幕。

我们程序中设置精灵的坐标时候用到的也应该是这俩个参数,因为我们在实际的写游戏项目的时候都不会写成死的坐标,而是一种相对的位置,比如我们经常通过getWinSize()获得屏幕的分辨率,然后设置精灵的位置在它的几分之几处


1      屏幕适配有不同的解决方案,都可以将解决问题
2      Ui编辑器中,所用的坐标逗的是%百分比。
写项目的时候都不会写成死的坐标,而是一种相对的位置

--------------------------------------------------------------------------------

键盘值得是?

键盘事件

除了键盘,设备上的菜单也可以使用这个监听器来处理事件。
上面的触碰事件(原文作者走神,说是鼠标事件),我们用了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, Event* event)
    {
        log("Key with keycode %d released", keyCode);

----------------------

 

Cocos2dx中文乱码问题

分类: Windows phone 8   4658人阅读  评论(2)  收藏  举报

最开始在网上找的一个方法,结果在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基本文件导致的,我们手动刚导入确实的文件即可,右键项目,点击属性,如下:

   image

    点击后面的下拉箭头选择 edit,在最后一行添加相应类库,

image

    再试试看~ 微笑

 

二、将显示文本单独保存为文本文件

    在cocos2d-x的示例项目中有关于配置文件读取的示例项目,有兴趣的童鞋可以自己去找下,这里将示例内容简化进行简要介绍。

    首先,相关配置文件必须放在项目Resource目录下,可自己设置二级目录,方便管理,

image

    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项读取出现显示,

image


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值