Click here to Skip to main content
15,885,638 members
Articles / Mobile Apps / Windows Phone 7

Back Button in XNA

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 May 2013CPOL2 min read 14.4K   5   2
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.

C#
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.

C#
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.

C#
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA13-Jun-13 20:51
professionalȘtefan-Mihai MOGA13-Jun-13 20:51 
GeneralRe: My vote of 5 Pin
zoyobar21-Jun-13 22:22
zoyobar21-Jun-13 22:22 

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

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