共用方式為


Managing Player Movement Between Lobby and Gameplay Modes

Describes properties and events associated with a multiplayer session that allow a game to move between lobby and gameplay modes. It identifies the game's current mode.

The Complete Sample

The code in the topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download NetworkingSample.

Managing Player Movement Between Modes

To manage player movement between lobby and gameplay modes

  1. Hook up the session state change events.

    The NetworkSession.GameStarted event indicates that all players have moved from the lobby to gameplay. The NetworkSession.GameEnded event occurs when players move from gameplay back to the lobby. NetworkSession.SessionEnded occurs when the session is over, and all players move from the lobby of a session back to find another available session.

    session.GameStarted += 
        new EventHandler<GameStartedEventArgs>(session_GameStarted);
    session.GameEnded += 
        new EventHandler<GameEndedEventArgs>(session_GameEnded);
    session.SessionEnded += 
        new EventHandler<NetworkSessionEndedEventArgs>(
            session_SessionEnded);
    
  2. Use NetworkGamer.IsReady to signal that a player is ready to move from the lobby to the game.

    // Signal I'm ready to play!
    if (IsButtonPressed(GamePadButton.A))
    {
        foreach (LocalNetworkGamer gamer in session.LocalGamers)
            gamer.IsReady = true;
    
    }
    
  3. On the host gaming machine, check NetworkSession.IsEveryoneReady to determine if all players are ready.

  4. If this is true, call NetworkSession.StartGame to change the game state from lobby to gameplay mode.

    // The host checks if everyone is ready, 
    // and moves to game play if true.
    if (session.IsHost)
    {
        if (session.IsEveryoneReady)
        {
            session.StartGame();
            foreach (SignedInGamer signedInGamer in 
                SignedInGamer.SignedInGamers)
            {
                signedInGamer.Presence.PresenceMode = 
                    GamerPresenceMode.InCombat;
            }
        }
    }
    
  5. In the NetworkSession.GameStarted event handler, make preparations for the game to begin.

    In this example, the game object states are reset for the new game.

    void session_GameStarted(object sender, GameStartedEventArgs e)
    {
    
        //// Reset everything when we are starting a new game.
        NetworkSession session = (NetworkSession)sender;
    
        for (int i = 0; i < session.AllGamers.Count; i++)
        {
            Tank tank = session.AllGamers[i].Tag as Tank;
            tank.Reset(i);
        }
    }
    
  6. In the NetworkSession.GameEnded event handler, make preparations—such as recording the high score—to return players to the lobby.

    void session_GameEnded(object sender, GameEndedEventArgs e)
    {
        ReturnToLobby();            
    }
    
  7. In the NetworkSession.SessionEnded event handler, return to the game title screen.

    void session_SessionEnded(object sender, NetworkSessionEndedEventArgs e)
    {
        ReturnToTitleScreen(); 
    }
    

    Note

    Before the session ends, it can move between lobby and gameplay states as many times as the player wants. To retrieve the session state at any time, check the NetworkSession.SessionState property.

See Also

Concepts

Network Session Management

Tasks

Creating a Network Session
Finding and Joining a Network Session
Joining and Leaving a Game