728x90 AdSpace

Latest Article

Keyboard event handling in cocos2d-x in C++ using VS 2010

Keyboard event handling in cocos2d-x in C++ using VS 2010 key Arrow key movement in cocos2dx?

Here is a nice tutorial and sample code that you can use for key event detection.
Along with it is the code for whole code of CCEGL_win32 class given, you just have to understand how key events are detected and make it implement how your scene changes at a key event.

The above sample link shared is simply passing a key identifier, wparam in the view id property of a CCTouch via SetTouchInfo. You can pass this param any other way you like or better still modify the code properly. For that you'll need to do the following: 

1) Add setIsKeypadEnabled(true); to your layer.

2) Modify the CCKeypadDispatcher.cpp class and add callback calls for the various keys you're handling. Right now it just handles the back button and the menu button in the dispatchKeypadMSG method by calling its delegates (CCKeypadDelegate's i.e., your layer's keyBackClicked() and keyMenuClicked() methods). Add methods to the CCKeypadDelegate.h to handle right, left, up and down methods and call those from the CCKeyPadDispatcher's dispatchKeypadMSG method by adding them to the current switch statement. Also add new fields in the ccKeypadMSGType enum in CCKeypadDispatcher.h for each direction. 

3) Now finally pass in one of thee keys via the CCEGLView_win32.cpp class' WindowProc method. Search for kTypeBackClicked. You'll see that a comparison is being made for wParam == VK_F1 and then one of the types in ccKeypadMSGType is being passed in to the dispatcher's dispatchKeypadMSG method. Test whether wParam is equal to VK_LEFT, VK_RIGHT, VK_UP and VK_DOWN and pass the appropriate new fields you've added in ccKeypadMSGType so they can be checked in the dispatchKeypadMSG method and the appropriate delegate method called. 

Finally the layer in which you've called setIsKeypadEnabled(true) you should make sure to implement the delegate methods to handle each keyboard input.

no image
  • Title : Keyboard event handling in cocos2d-x in C++ using VS 2010
  • Posted by :
  • Date : 10:55
  • Labels :

  • Blogger Comments
  • Facebook Comments


Post a Comment