Click here to Skip to main content
Click here to Skip to main content

Back Button in XNA

, 7 May 2013
Rate this:
Please Sign up or sign in to vote.
I will explore and learn XNA for Windows Phone with you.

The original post can be found here.

Introduction/Catalog

If you want to create a game for Windows Phone, then XNA is a choice. Now, I will explore and learn XNA for Windows Phone with you.

  • Marketplace Review
  • Capture the Back Button
  • Ask the User
  • Windows Phone 7.x
  • Marketplace Review

If you want your games pass the review of Windows Phone Marketplace, then you need to do some special processing for Back button.

If the user presses the Back button when playing, you should ask the user whether to exit current level and pause the game.

If the user presses the Back button on the home screen, then you should ask the user whether or not to quit the game.

Capture the Back Button

In the default case, the class Game in your project already contains codes about the Back button.

protected override void Update ( GameTime gameTime )
{

 if ( GamePad.GetState ( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
  this.Exit ( );

 base.Update ( gameTime );
}

GamePad is a class for game pad. Use GetState method to get a player's game pad. But in Windows Phone, we just need to get the game pad of player I.

The GetState method returns the class GamePadState, which is the game pad status. Property Buttons is the buttons on the game pad, in the above code, we get the property Back, which is the state of the Back button. If the Back button is pressed, then we exit the game.

Ask the User

In the above code, if you press the Back button, then you will exit the game. So we need to add some code that asks the user whether to exit.

protected override void Update ( GameTime gameTime )
{
 
 if ( GamePad.GetState ( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
  Guide.BeginShowMessageBox ( "Exit", "Do you want to exit?", 
  new string[] { "Yes", "No" }, 1, 
  MessageBoxIcon.None, new AsyncCallback ( this.userSelected ), null );

 base.Update ( gameTime );
}

private void userSelected ( IAsyncResult result )
{

 if ( !result.IsCompleted )
  return;

 int? index = Guide.EndShowMessageBox ( result );

 if ( index.HasValue && index.Value == 0 )
  this.Exit ( );

}

We use the BeginShowMessageBox method of Guide class to pop up a dialog box that lets the user choose whether or not to quit the game. The method userSelected is used to determine the user's choice. They can choose Yes or No.

In the method userSelected, parameter result indicates the user's selection, property IsCompleted indicates whether the user completed the selection. You can use EndShowMessageBox method of Guide to get the button index. In our code, and 0 means Yes. Note the variable index, it is not int, but is int?.

We can add two more fields to represent whether the player is playing and the game is paused. According to these two variables to display different dialog boxes.

Windows Phone 7.x

On Windows Phone 7.x devices, we pop up a dialog box when the user presses the Back button, if users press the Back button again before the dialog box is displayed, the BeginShowMessageBox might throw an exception. If you want to avoid this error, you can set a field to determine whether the dialog box is displayed.

private bool isMessageBoxShow = false;

protected override void Update ( GameTime gameTime )
{

 if ( GamePad.GetState ( PlayerIndex.One ).Buttons.Back == 
 ButtonState.Pressed && !this.isMessageBoxShow )
 {
  this.isMessageBoxShow = true;
  Guide.BeginShowMessageBox ( "Exit", "Do you want to exit?", new string[] 
  { "Yes", "No" }, 1, MessageBoxIcon.None, 
  new AsyncCallback ( this.userSelected ), null );
 }

 base.Update ( gameTime );
}

private void userSelected ( IAsyncResult result )
{
 this.isMessageBoxShow = false;

 if ( !result.IsCompleted )
  return;

 int? index = Guide.EndShowMessageBox ( result );

 if ( index.HasValue && index.Value == 0 )
  this.Exit ( );

}

For more contents, please visit Developer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

zoyobar

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMihai MOGA13-Jun-13 20:51 
GeneralRe: My vote of 5 Pinmemberzoyobar21-Jun-13 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 8 May 2013
Article Copyright 2013 by zoyobar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid