Problem
Before you can connect to another player, you must use an account to log on or create a new account. If all players are on the same network, this can be an offline account or an online live account when you want to connect over the Internet.
You must log on before you can access the network function of xNa. It also allows other players to see your name and other information you may provide.
Note:You do not need to log on to Zune. The xNa Game On Zune starts with a signedinplayer, which owns the name you allocated to the Zune device.
Solution
It is very easy to log on with your account. All you need to do is call guide. showsignin, which allows you to select an existing account or create a new one.
Working Principle
Before you can use the xNa network function, you must add gamerservicescomponent to the game.
Add gamerservicescomponent
This gamecomponent (see tutorial 1-7) ensures that the entire network engine is updated at a constant interval. For any gamecomponent, it must be loaded in the game constructor:
Public game1 ()... {graphics = new graphicsdevicemanager (this); content. rootdirectory = "content"; components. Add (New gamerservicescomponent (this ));}
Display logon page
SimpleProgramSwitches between the two States defined: signin and signedin. As long as the program is in the signin status, the logon interface is displayed to the user until the user selects or creates an account. Once you select the correct account, the program will be moved to the signedin status.
First, declare two states:
Public Enum gamestate... {signin, signedin}
Then declare a variable to save the current state:
Gamestate currentgamestate = gamestate. signin;
This puts the program in the signin state at the beginning. In the update loop, you will check the status of the program. If you are in the signin status, you want to select an account. However, because you can select an account to log on automatically, you may have logged on to an account when you start gamerservicescomponent. Therefore, you want to check whether there is no account for the current Logon:
If (gamer. signedingamers. count <1 )... {guide. showsignin (1, false); log. add ("opened user signin interface");} else... {currentgamestate = gamestate. signedin; log. add (gamer. signedingamers [0]. gamertag + "logged in-Proceed to signedin ");}
If no account is recorded, You can activate the logon interface so that you can select or create an account. Once an account is selected, the value of gamer. signedingamers. Count is not 0. In the next update process, the current status is set to signedin, and information containing the player name is added to the log.
Note:The log Content is output to the screen in the draw method. For more information, see tutorial 3-5 to learn how to display text on the screen.
The Guide. showsignin method requires two parameters. The first parameter shows the number of players that can log on. On the Xbox 360 platform, a maximum of four users are allowed at the same time, and only one user is allowed on the Windows platform. The second parameter specifies whether only online live account logon is allowed, which is useful when using the Live service.
Note:Remember that you can cancel the guide interface, which leads to no account being selected.
Because the guide interface cannot be opened twice at the same time, you can call the guide. before using the showsignin method, you must check whether the current Guide is closed. This can be done by checking the isactive attribute of the game. When the game window is active, it is not minimized, this attribute is true only when the Guide is not displayed.
Code
The entire update process is as follows. Check the isactive attribute first, and then call the Guide. signin method if you have not logged on with an account. Once you log on to the correct account, the status changes to signedin.
protected override void Update (gametime )... {If (gamepad. getstate (playerindex. one ). buttons. back = buttonstate. pressed) This. exit (); If (this. isactive )... {Switch (currentgamestate )... {Case gamestate. signin :... {If (gamer. signedingamers. count <1 )... {guide. showsignin (1, false); log. add ("opened user signin interface");} else... {currentgamestate = gamestate. signedin; log. add (gamer. signedingamers [0]. gamertag + "logged in-Proceed to signedin") ;}} break; Case gamestate. signedin :... {} break;} base. update (gametime) ;}