cocos2d-x中添加TV按键响应

前端之家收集整理的这篇文章主要介绍了cocos2d-x中添加TV按键响应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近有个机顶盒项目,需要用到遥控器的方向键在游戏中控制方向。

如果是一般Android设备、iOS设备或Win32平台,cocos2d-x提供了“CCAccelerometer”类,可用于游戏控制UI。但由于机顶盒在使用过程中是使用遥控器与玩家交互,而遥控器中缺少GSenser,所以只能暂时使用方向键和中键。好在机顶盒厂家使用了Android标准的键值,方向键和中键与Andriod DPAD按键一一对应。

下面讲一下如何修改cocos2d-x,以使我们在游戏中能感知DPAD按键消息。

修改cocos2dx\keypad_dispatcher\CCKeypadDelegate.h

为” CCKeypadDelegate”添加2个虚函数,完成后为:

  1. classCC_DLLCCKeypadDelegate
  2. {
  3. public:
  4. //Thebackkeyclicked
  5. virtualvoidkeyBackClicked(){}
  6. //Themenukeyclicked.onlyavailableonwophone&android
  7. voidkeyMenuClicked(){};
  8. //Theenterkeyclicked.onlyavailableonwin32&android
  9. voidkeyEnterClicked(){};
  10. //Thearrowkeyclicked.onlyavailableonwin32&android
  11. voidkeyArrowClicked(intarrow){};
  12. };

其中” keyEnterClicked”函数用于响应”Enter”键,”keyArrowClicked”函数用于响应方向键消息。

二 cocos2dx\keypad_dispatcher\CCKeypadDispatcher.h

修改枚举 ccKeypadMSGType的定义为:

copy

    typedefenum{
  1. //thebackkeyclickedmsg
  2. kTypeBackClicked=1,
  3. //themenukeyclickedmsg
  4. kTypeMenuClicked,0); background-color:inherit">//theEnterkeyclickedmsg
  5. kTypeEnterClicked,0); background-color:inherit">//thearrowkeyclickedmsg
  6. kTypeLeftArrowClicked,248)"> kTypeUpArrowClicked,
  7. kTypeRightArrowClicked,248)"> kTypeDownArrowClicked,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }ccKeypadMSGType;

三 cocos2dx\keypad_dispatcher\CCKeypadDispatcher.cpp
修改” dispatchKeypadMSG”函数,在:

copy

    casekTypeMenuClicked:
  1. pDelegate->keyMenuClicked();

添加

copy

    casekTypeEnterClicked:
  1. pDelegate->keyEnterClicked();
  2. break;
  3. casekTypeLeftArrowClicked:
  4. casekTypeUpArrowClicked:
  5. casekTypeRightArrowClicked:
  6. casekTypeDownArrowClicked:
  7. pDelegate->keyArrowClicked(nMsgType);
  8. break;

针对Android平台还需要一下修改
cocos2dx\platform\android\java\src\org\cocos2dx\lib\Cocos2dxGLSurfaceView.java
找到” onKeyDown”函数,在其中添加几个键值处理,使函数如下:

[java] copy
    @Override
  1. publicbooleanonKeyDown(finalintpKeyCode,finalKeyEventpKeyEvent){
  2. switch(pKeyCode){
  3. caseKeyEvent.KEYCODE_BACK:
  4. caseKeyEvent.KEYCODE_MENU:
  5. caseKeyEvent.KEYCODE_DPAD_UP://19
  6. caseKeyEvent.KEYCODE_DPAD_DOWN://20
  7. caseKeyEvent.KEYCODE_DPAD_LEFT://21
  8. caseKeyEvent.KEYCODE_DPAD_RIGHT://22
  9. caseKeyEvent.KEYCODE_DPAD_CENTER://23
  10. this.queueEvent(newRunnable(){
  11. @Override
  12. voidrun(){
  13. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
  14. }
  15. });
  16. returntrue;
  17. default:
  18. super.onKeyDown(pKeyCode,pKeyEvent);
  19. }
  20. }
*注意 :项目目录中proj.android\src\org\cocos2dx\lib\Cocos2dxGLSurfaceView.java需要修改与此相同。

五 cocos2dx\platform\android\jni\TouchesJni.cpp

找到:

copy

    #defineKEYCODE_BACK0x04
  1. #defineKEYCODE_MENU0x52

紧接着在下面添加

copy

    #defineKEYCODE_DPAD_UP19
  1. #defineKEYCODE_DPAD_DOWN20
  2. #defineKEYCODE_DPAD_LEFT21
  3. #defineKEYCODE_DPAD_RIGHT22
  4. #defineKEYCODE_DPAD_CENTER23
然后修改”Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown”函数,如下:

copy

    JNIEXPORTjbooleanJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv*env,jobjectthiz,jintkeyCode){
  1. CCDirector*pDirector=CCDirector::sharedDirector();
  2. switch(keyCode){
  3. caseKEYCODE_BACK:
  4. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked))
  5. returnJNI_TRUE;
  6. caseKEYCODE_MENU:
  7. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeMenuClicked))
  8. caseKEYCODE_DPAD_UP:
  9. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeUpArrowClicked))
  10. caseKEYCODE_DPAD_DOWN:
  11. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeDownArrowClicked))
  12. caseKEYCODE_DPAD_LEFT:
  13. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeLeftArrowClicked))
  14. caseKEYCODE_DPAD_RIGHT:
  15. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeRightArrowClicked))
  16. caseKEYCODE_DPAD_CENTER:
  17. if(pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeEnterClicked))
  18. returnJNI_FALSE;
  19. }
至此Android平台修改完成。
为了方便我们在VS2010中开发,还有针对Win32平台进行修改
六 cocos2dx\platform\win32\CCEGLView.cpp

找到”WindowProc”函数,找到:

copy

    if(wParam==VK_F1||wParam==VK_F2)
  1. CCDirector*pDirector=CCDirector::sharedDirector();
  2. if(GetKeyState(VK_LSHIFT)<0||GetKeyState(VK_RSHIFT)<0||GetKeyState(VK_SHIFT)<0)
  3. pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam==VK_F1?kTypeBackClicked:kTypeMenuClicked);
  4. elseif(wParam==VK_ESCAPE)
  5. CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);
  6. 紧接着在后边添加

    copy

      if(wParam==VK_RETURN)
    1. CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeEnterClicked);
    2. if(wParam>=VK_LEFT&&wParam<=VK_DOWN)
    3. CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG((ccKeypadMSGType)(kTypeLeftArrowClicked+wParam-VK_LEFT));
    4. 好,现在Win32平台也添加完成了,程序中可以响应键盘上的方向键和回车键。

      使用方法如下:
      假设我们要在继承自CCLayer的HelloWorld中相应按键,那么首先在HelloWorld的头文件添加两个函数:

      copy

        intarrow);
      1. voidkeyEnterClicked();
      然后,在HelloWorld初始化时启用按键功能

      copy

        setKeypadEnabled(true);
      在HelloWorld中实现之前声明的2个虚函数

      copy

        voidLLPlaneControlLayer::keyArrowClicked(intarrow)
      1. if(arrow==kTypeLeftArrowClicked)
      2. //左方向键按下
      3. if(arrow==kTypeRightArrowClicked)
      4. //右方向键按下
      5. if(arrow==kTypeUpArrowClicked)
      6. {
      7. //上方向键按下
      8. if(arrow==kTypeDownArrowClicked)
      9. //下方向键按下
      10. voidLLPlaneControlLayer::keyEnterClicked()
      11. //Enter键按下
      12. 用户按下方向键,keyArrowClicked会被执行,参数arrow表示按下的键;当用户按下Enter键(Android平台为DPAD_CENTER键),keyEnterClicked函数会执行。

        猜你在找的Cocos2d-x相关文章