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

前端之家收集整理的这篇文章主要介绍了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

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