1
\$\begingroup\$

I am developing a simple game for windows phone and I got stuck when at the handling the back button press.

I have been searching for a while on google but I have not found anything that could work for me. I have seen a function GamePad but i couldn't find on which namespace is this function located, and if it is working on WP8.1.

So my question is, how can I set up a function in which I can handle some code when user press back button.

Thanks.

\$\endgroup\$
  • \$\begingroup\$ stackoverflow.com/questions/14466938/… \$\endgroup\$ – dimitris93 Feb 1 '15 at 10:28
  • 1
    \$\begingroup\$ Yes, but when I try to override the the OnNavigatedFrom method on build i get this error: 'Error 1 Game1.GamePage.OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs)' is a new virtual member in sealed class 'Game1.GamePage' I am using the new Monogame template for windows phone 8.1 which is not silverlight but runtime. \$\endgroup\$ – Edvin Feb 1 '15 at 13:58
0
\$\begingroup\$

I too couldn't wrap my head around this, as there is no mention of it in the documentation whatsoever. I also tried every single thing I could google and in the end, I figured it out by chance.

It's quite simple, really:

GamePadState padState = GamePad.GetState(PlayerIndex.One);
if (padState.Buttons.Back == ButtonState.Pressed) {
    // Do your stuff
}

BUT!

I had some major issues with it for what appeared to be two incomprehensibly related reasons: firstly, be sure to call this in your update as soon as you can. Calling it on the end of my update method made it work only sometimes (probably for some other reason, but I couldn't figure it out).

And secondly, keep one global instance of GamePadStatefor use in all classes that might have some control logic - I had two classes that registered control (with their own GamePadState instances) and while one did register the back button, the other didn't - and when I "paused" the update of the first one, the second one started to register it. Beats me.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.