Saturday, December 1, 2007

Getting your game ready to support multiplayer.

Ok, so whats the point of using XNA 2 over XNA 1 if we not going to implement its major new feature... Network (Xbox Live) Support!

So lets modify the Game State Management sample a bit to make it more suitable for a Network game.

  1. Open the project you downloaded earlier (see previous post)
  2. Edit the MainMenuScreen.cs file (under Screens)
Modify the MainMenuScreen() to look like this:
/// <summary>
/// Constructor fills in the menu contents.
/// </summary>
public MainMenuScreen()
{
MenuEntries.Add("Single Player");
MenuEntries.Add("Multiplayer");
MenuEntries.Add("Options");
MenuEntries.Add("Exit");
}

Modify the OnSelectEntry(int entryIndex) to look like this:

/// <summary>
/// Responds to user menu selections.
/// </summary>
protected override void OnSelectEntry(int entryIndex)
{
switch (entryIndex)
{
case 0:

// Play the game in single player mode
LoadingScreen.Load(ScreenManager, LoadGameplayScreen, true);
break;

case 1:

// Go to multiplayer selection screen
// On this screen you will select either on Local or Live
ScreenManager.AddScreen(new LiveOptionsMenuScreen());
break;

case 2:

// Go to the options screen.
ScreenManager.AddScreen(new OptionsMenuScreen());
break;

case 3:

// Exit the sample.
OnCancel();
break;
}
}

3. Create a new class under the Screens subfolder called "LiveOptionsMenuScreen.cs"
4. Edit the LiveOptionsMenuScreen.cs file:

#region Using Statements
using Microsoft.Xna.Framework;
#endregion

namespace GameStateManagement
{
class LiveOptionsMenuScreen : MenuScreen
{
/// <summary>
/// Constructor populates the menu with empty strings: the real values
/// are filled in by the Update method to reflect the changing settings.
/// </summary>
public LiveOptionsMenuScreen()
{
MenuEntries.Add("XBox LIVE");
MenuEntries.Add("Split Screen");
MenuEntries.Add("System Link");
MenuEntries.Add("Back");
}

/// <summary>
/// Responds to user menu selections.
/// </summary>
protected override void OnSelectEntry(int entryIndex)
{
switch (entryIndex)
{
case 0:
// XBox LIVE

break;

case 1:
// Split Screen

break;

case 2:
// System Link

break;

case 3:
// Back
// Go back to the main menu.
ExitScreen();
break;
}
}

/// <summary>
/// When the user cancels the options screen, go back to the main menu.
/// </summary>
protected override void OnCancel()
{
ExitScreen();
}
}
}

Now if you press F5 to Build the game you should see something like this:

Clicking Multiplayer should now take you to a screen like this:



No comments: