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

Back button in XNA

By , 7 May 2013
 

The original: http://www.wpgamer.info/2013/05/back-button-in-xna.html

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 press the Back button when playing, you should ask the user whether to exit current level and pause the game.

If the user press 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. Using 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 codes that ask 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 ( );

}

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130513.1 | Last Updated 8 May 2013
Article Copyright 2013 by zoyobar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid