微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2d-x接受键盘事件,左右键, 多点触摸

首先在AppDelegate.cpp加入以下代码,一定要在AppDelegate::applicationDidFinishLaunching()上,声明用的。

[html] view plain copy
  1. #if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
  2. HelloWorld*g_layer;
  3. voidWin32SetKeyLayer(HelloWorld*layer)
  4. {
  5. g_layer=layer;
  6. }
  7. voidWin32KeyHook(UINTmessage,WParaMwParam,LParaMlParam)
  8. {
  9. cclog("Win32KeyHookmessage%dwParam%dlParam%d",message,wParam,lParam);
  10. if(g_layer)
  11. g_layer->onWin32KeyEvent(message,lParam);
  12. }
  13. #endif

在AppDelegate::applicationDidFinishLaunching()中

copy

    boolAppDelegate::applicationDidFinishLaunching(){
  1. //initializedirector
  2. CCDirector*pDirector=CCDirector::sharedDirector();
  3. CCEGLView*pEGLView=CCEGLView::sharedOpenGLView();
  4. #if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
  5. //2012.11.07加入键盘处理代码
  6. pEGLView->setAccelerometerKeyHook(Win32KeyHook);///////////////////////////////////
  7. #endif//CC_PLATFORM_WIN32
  8. pDirector->setopenGLView(pEGLView);
  9. //turnondisplayFPS
  10. pDirector->setdisplayStats(true);
  11. //setFPS.thedefaultvalueis1.0/60ifyoudon'tcallthis
  12. >setAnimationInterval(1.0/60);
  13. //createascene.it'sanautoreleaSEObject
  14. CCScene*pScene=HelloWorld::scene();
  15. //run
  16. >runWithScene(pScene);
  17. returntrue;
  18. }

在HelloWorldScene.cpp中

copy

    CCScene*HelloWorld::scene()
  1. //'scene'isanautoreleaSEObject
  2. CCScene*scene=CCScene::create();
  3. //'layer'isanautoreleaSEObject
  4. HelloWorld*layer=HelloWorld::create();
  5. //addlayerasachildtoscene
  6. scene->addChild(layer);
  7. externvoidWin32SetKeyLayer(HelloWorld*layer);
  8. Win32SetKeyLayer(layer);
  9. //returnthescene
  10. returnscene;
  11. }

copy
    voidHelloWorld::onWin32KeyEvent(UINTmessage,LParaMlParam)
  1. cclog("onWin32KeyEventmessage%dwParam%dlParam%d",lParam);
  2. /*
  3. //Up
  4. Win32KeyHookmessage256wParam38lParam21495809
  5. onWin32KeyEventmessage256wParam38lParam21495809
  6. Win32KeyHookmessage257wParam38lParam-1052246015
  7. onWin32KeyEventmessage257wParam38lParam-1052246015
  8. //Down
  9. Win32KeyHookmessage256wParam40lParam22020097
  10. onWin32KeyEventmessage256wParam40lParam22020097
  11. Win32KeyHookmessage257wParam40lParam-1051721727
  12. onWin32KeyEventmessage257wParam40lParam-1051721727
  13. //Left
  14. Win32KeyHookmessage256wParam37lParam21692417
  15. onWin32KeyEventmessage256wParam37lParam21692417
  16. Win32KeyHookmessage257wParam37lParam-1052049407
  17. onWin32KeyEventmessage257wParam37lParam-1052049407
  18. //Right
  19. Win32KeyHookmessage256wParam39lParam21823489
  20. onWin32KeyEventmessage256wParam39lParam21823489
  21. Win32KeyHookmessage257wParam39lParam-1051918335
  22. onWin32KeyEventmessage257wParam39lParam-1051918335
  23. */
  24. if(message==256)
  25. switch(wParam)
  26. case38:
  27. moveHero(1);
  28. break;
  29. case40:
  30. moveHero(2);
  31. break;
  32. case37:
  33. moveHero(3);
  34. case39:
  35. moveHero(4);
  36. elseif(message==257)
  37. copy
      voidHelloWorld::moveHero(intdiraction)
    1. cclog("moveHero:%d",diraction);
    2. //现在你就可以上下左右键,看输出的值。


      http://www.vktone.com/articles/win32_key_process_in_cocos2dx.html

      原文地址:https://www.jb51.cc/cocos2dx/343114.html

      版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

      相关推荐