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

来源:互联网 发布:淘宝可以开发票吗 编辑:程序博客网 时间:2024/04/25 05:09

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

[html] view plaincopy
  1. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  2. HelloWorld *g_layer;  
  3.   
  4. void Win32SetKeyLayer(HelloWorld *layer)  
  5. {  
  6.     g_layer = layer;  
  7. }  
  8.   
  9. void Win32KeyHook( UINT message,WPARAM wParam, LPARAM lParam )  
  10. {  
  11.     CCLog("Win32KeyHook message %d wParam %d lParam %d", message, wParam, lParam);  
  12.     if (g_layer)  
  13.         g_layer->onWin32KeyEvent(message, wParam, lParam);  
  14. }  
  15. #endif  

在AppDelegate::applicationDidFinishLaunching()中

[html] view plaincopy
  1. bool AppDelegate::applicationDidFinishLaunching() {  
  2.   
  3.   
  4.   
  5.     // initialize director  
  6.     CCDirector* pDirector = CCDirector::sharedDirector();  
  7.     CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();  
  8.   
  9. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  10.     // 2012.11.07 加入键盘处理代码  
  11.     pEGLView->setAccelerometerKeyHook(Win32KeyHook);///////////////////////////////////  
  12. #endif  // CC_PLATFORM_WIN32  
  13.   
  14.   
  15.     pDirector->setOpenGLView(pEGLView);  
  16.       
  17.     // turn on display FPS  
  18.     pDirector->setDisplayStats(true);  
  19.   
  20.     // set FPS. the default value is 1.0/60 if you don't call this  
  21.     pDirector->setAnimationInterval(1.0 / 60);  
  22.   
  23.     // create a scene. it's an autorelease object  
  24.     CCScene *pScene = HelloWorld::scene();  
  25.   
  26.     // run  
  27.     pDirector->runWithScene(pScene);  
  28.   
  29.     return true;  
  30. }  

在HelloWorldScene.cpp中

[html] view plaincopy
  1. CCScene* HelloWorld::scene()  
  2. {  
  3.     // 'scene' is an autorelease object  
  4.     CCScene *scene = CCScene::create();  
  5.       
  6.     // 'layer' is an autorelease object  
  7.     HelloWorld *layer = HelloWorld::create();  
  8.   
  9.     // add layer as a child to scene  
  10.     scene->addChild(layer);  
  11.   
  12.     extern void Win32SetKeyLayer(HelloWorld *layer);  
  13.     Win32SetKeyLayer(layer);  
  14.   
  15.     // return the scene  
  16.     return scene;  
  17. }  

[html] view plaincopy
  1. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  2. void HelloWorld::onWin32KeyEvent( UINT message,WPARAM wParam, LPARAM lParam )  
  3. {  
  4.      CCLog("onWin32KeyEvent message %d wParam %d lParam %d", message, wParam, lParam);  
  5.     /*  
  6.     // Up  
  7.     Win32KeyHook message 256 wParam 38 lParam 21495809  
  8.     onWin32KeyEvent message 256 wParam 38 lParam 21495809  
  9.     Win32KeyHook message 257 wParam 38 lParam -1052246015  
  10.     onWin32KeyEvent message 257 wParam 38 lParam -1052246015  
  11.     // Down  
  12.     Win32KeyHook message 256 wParam 40 lParam 22020097  
  13.     onWin32KeyEvent message 256 wParam 40 lParam 22020097  
  14.     Win32KeyHook message 257 wParam 40 lParam -1051721727  
  15.     onWin32KeyEvent message 257 wParam 40 lParam -1051721727  
  16.     // Left  
  17.     Win32KeyHook message 256 wParam 37 lParam 21692417  
  18.     onWin32KeyEvent message 256 wParam 37 lParam 21692417  
  19.     Win32KeyHook message 257 wParam 37 lParam -1052049407  
  20.     onWin32KeyEvent message 257 wParam 37 lParam -1052049407  
  21.     // Right  
  22.     Win32KeyHook message 256 wParam 39 lParam 21823489  
  23.     onWin32KeyEvent message 256 wParam 39 lParam 21823489  
  24.     Win32KeyHook message 257 wParam 39 lParam -1051918335  
  25.     onWin32KeyEvent message 257 wParam 39 lParam -1051918335  
  26.     */  
  27.     if (message == 256)  
  28.     {  
  29.         switch (wParam)  
  30.         {  
  31.         case 38:  
  32.             moveHero(1);  
  33.             break;  
  34.         case 40:  
  35.             moveHero(2);  
  36.             break;  
  37.         case 37:  
  38.             moveHero(3);  
  39.             break;  
  40.         case 39:  
  41.             moveHero(4);  
  42.             break;  
  43.         }  
  44.     }  
  45.     else if (message == 257)  
  46.     {  
  47.     }  
  48. }  
  49. #endif  

[html] view plaincopy
  1. void HelloWorld::moveHero( int diraction )  
  2. {  
  3.     CCLog("moveHero: %d",diraction);  
  4. }  

//现在你就可以上下左右键,看输出的值。



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

0 0
原创粉丝点击