Monday 20 January 2014

Handle back key event in Cocos2dx 2.2.2 Windows Phone 8 XAML and C++

Cocos2d-x-2.2.2 version has introduced new project template based on XAML and C++ Component  for Windows Phone 8 platform which allows you to develop games with C# and C++ mixed. You can use XAML components using this template but it's missing BACK key event handling which is primary key for Windows Phone application navigation. However there is a work around which allows you to handle BACK key in your C++ Component based game.

Open CocosRenderer.cpp and change the OnBackKeyPress() method as following:

bool Cocos2dRenderer::OnBackKeyPress()
{
    //return false;
    CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);
    return true;
}



You can override virtual void keyBackClicked();  method in your Game Layer and handle back key event.

10 comments :

  1. Hello vikas,
    i tried the solution for back key handling but it is throwing me some run time exception.... but anyhow i have managed to handle back key handling my way...
    The main problem i got stuck into is that i am not getting any way to call c# method from cpp method. I am doing this so as to integrate ads, facebook n in-app purchase...
    Plz if you got any solution plz let me know...

    ReplyDelete
    Replies
    1. Hello have a look at this article.
      http://www.developer.nokia.com/community/wiki/Writing_interfaces_that_can_be_implemented_by_both_Native_and_Managed_classes

      Delete
    2. I am also getting this:
      {System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.}

      How did you solve this Suveer Jacob ?

      Delete
    3. In my case there was some threading problem.... we cannot easily access xaml features from cpp or vice versa..
      This is how i managed:

      1-> Add following class in 'InputEvent.h'

      class BackkeyEvent : public InputEvent
      {
      public:
      virtual void execute(Cocos2dRenderer ^ renderer);
      };

      2->Implement the virtual method in 'InputEvent.cpp'

      void BackkeyEvent::execute(Cocos2dRenderer ^ renderer )
      {
      renderer->OnBackKeyPress();
      }

      3-> Add the following code in 'Direct3DInterop::OnBackKeyPress()'

      bool Direct3DInterop::OnBackKeyPress()
      {
      std::lock_guard guard(mMutex);
      std::shared_ptr e(new BackkeyEvent());
      mInputEvents.push(e);
      return true;
      }

      and then the solution vikas patidar gave will work...

      This was in my case... My backkey button was also throwing some exception i tried the solution n it worked. I hope it might work in your case too..

      Delete
  2. Hello vikas
    nice work but your solution has a problem if you're in the first scene and loading up the back button does not close the application.

    ReplyDelete
    Replies
    1. You have to close your application from MainPage.xaml. You can just set various states in your game and using the reference to Direct3DInterop object you can find the game state in your C# module and then you can close your app.

      Delete
  3. there is no xaml project in the projectname folder... how do you even get it?

    ReplyDelete
    Replies
    1. Check for Cocos2dx 2.2.2 version, there is a template inside folder

      "cocos2d-x-2.2.2\samples\Cpp\HelloCpp\proj.wp8-xaml"

      Delete
  4. ya thanks but how do i create my own xaml project in 2.2.2? the hellocpp xaml works fine... but could you help me figure out how to get it working in 2.2.2?

    ReplyDelete
  5. c# interview questions
    Its simply superb.Feeling good to share the link to practice csharpinterview questions @ http://skillgun.com

    ReplyDelete