Compile the apple game center application (translated by 1.18 To handle player status changes in multiplayer games)

Source: Internet
Author: User

 

1.18 handle player status changes in multiplayer games

Problem

You want to detect when a player disconnects in multiplayer mode.

Solution

Implementation and ProcessingGkmatchdelegateClassMatch: Player: didchangestate:Host messages.

Discussion

In multiplayer games, it is important for a player to know the status of other players. The status here indicates a connection or disconnection.

Assume that you have compiled a racing game and used matchmaking. After the two players connected, they started the first lap of the championship. Suddenly, player #2 is disconnected. In this case, you must notify the player of the message #1 and end the match so that player #1 can wait for other invitations.

Implement and handle changes to player statusGkmatchdelegateClassMatch: Player: didchangestate:Host messages. The parameter Player contains the ID of the player whose ID is changed.Didchangestate:Parameters (GkplayerconnectionstateType), will contain one of the following values:

Gkplayerstateunknown

The player status is unknown. In a racing game, you may want to temporarily keep the player's car position unchanged.

Gkplayerstateconnected

Player connection. You may want to display a message (for example, "Player 2 is connected. Let's go !") To local players.

Gkplayerstatedisconnected

Players are disconnected. This status determines whether to stop the game, return to the main menu to wait for another match, and temporarily pause the game.

You can useExpectedplayercountThe instance method knows that it takes more than one player to start the game. For example, if we start a game that requires two players and one of them is disconnectedExpectedplayercountThe return value is 1, indicating that another player is required to start the game again. In the following code, assume that we are in a match between two people, and if one of them is disconnected, we will stop the match.

/* The Player state changed
(Eg. connected or disconnected )*/
-(Void) Match :( gkmatch *) Match
Player :( nsstring *) playerid
Didchangestate :( gkplayerconnectionstate) State {

Switch (state ){

Case gkplayerstatedisconnected :{

If ([Match expectedplayercount]> 0 ){
[Match Disconnect];
}
Break;

}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.