Handling the back button on cocos2d-x

On cocos2d-x the back button isn’t default available you must code for it, and on windows phone and android are difference about back button code. In this Tip I’ll help you handle back button on both. Now let do it!

+) I’ll guide on windows phone first
- First you create a new project and open wp8 project (I’m using cocos2d-x 3.4).
If you have error like this:

You need do add cocos2d namespace by : using cocos2d; anh then go BUILD -> Clean Solution

- Go to MainPage.xaml.cs we will look it for a while
You will see this method
 protected override void OnBackKeyPress(CancelEventArgs e)
        {
            m_d3dInterop.OnBackKeyPress();
            // cocos2d-x will async send Cocos2dEvent.TerminateApp event if it is time to exit app.
            // We do not want to exit now, so we set e.Cancel to true.
            e.Cancel = true;
        }
Well, when you press back button on windows phone it will call OnBackKeyPress() in m_d3dInterop Object and m_d3dInterop is an instance of Direct3Dinterop we will find and code on this Object.

- Find Direct3Dinterop and Handle back button

On Solution Explorer (View -> Solution Explorer), find your C++ component (default it under C# project) expand src folder and you will see Direct3Dinterop Object then open it and find OnBackKeyPress() method. Because we can have a lot of Scene and you will want to handle back button for each scene so we will create class BackPressed to solve this.

-Create class BackPressed and inherit it
I create BackPressed class in HelloWorldScene.h file like this:
class BackPressed
{
 public:
  static BackPressed *myptr;

  BackPressed()
  {
   myptr = this;
  }

  virtual void backPressCalled()
  {

  }

  static BackPressed *getInstance()
  {
   return myptr;
  }
};
And in HelloWorldScene.cpp don’t forget do it with static variable
BackPressed* BackPressed::myptr = nullptr;

Go to Scene which you want to handle back button and inherit class BackPressed, with me I use HelloWorld class to inherit it.
class HelloWorld : public cocos2d::Layer, public BackPressed

and then override backPressCalled() method
void backPressCalled();

void HelloWorld::backPressCalled()
{
          //TODO code here
}
Well, I think you maybe want to exit Windows phone application when press back button. If you want to do it read this post to know how to call C# from C++ and you just need code on C#:
App.Current.Terminate();

+) On android
On android handling back button is easily. You just override method:
void onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event);
and code on it:
void PlayScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event)
{    
      if (keyCode == EventKeyboard::KeyCode::KEY_BACK)
      {
            //CODE HERE
      }
}

and you must enable keyboard:
add this code in init() function:
this->setKeyboardEnabled(true);
Download Project on Github:



Share on Google Plus

About Lộc Thọ

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 Comment:

Post a Comment