O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coding Your Game1 Class

The first thing you’ll need to do in your Game1 class is add an enum that you’ll use to represent game states. We’ve discussed game states in previous chapters, but they’re never more important than in networked games. Beyond the typical states in a game (a start state where you display instructions or splash screens, an in-game state, and an end-game state), in a networked game you’ll usually also have a sign-in state where the player signs into Xbox LIVE or Games for Windows LIVE, a state where you find sessions of your game, and a state where you create sessions.

You’ll actually want to add the following enum outside of the Game1 class, between the Catch namespace declaration and the class declaration. This will allow any other classes you may add later to access the game states more easily:

namespace Catch
{
    // Represents different states of the game
    public enum GameState { SignIn, FindSession,
        CreateSession, Start, InGame, GameOver }

    public class Game1 : Microsoft.Xna.Framework.Game
    {
       ...

In addition, you’ll need to add another enum that represents different types of messages that are sent across the network. Why? You need this because, as you’ll see shortly, when your game reads data from the network, it needs to know in advance what type of data is coming in (an int, a string, a Vector2, etc.). You’ll also need to know how much data is coming (two ints? three ints? one int and two strings?). That’s not a problem if you’re always sending the exact same ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required