1
我正在嘗試使用自定義UI(沒有GKMatchMakerViewController
)實現實時多人遊戲。我使用startBrowsingForNearbyPlayersWithReachableHandler: ^(NSString *playerID, BOOL reachable)
找到一個本地玩家,然後用GKMatchmaker
singleton(我已經發起)發起一個匹配請求。iOS Gamecenter Programmatic Matchmaking
這是我遇到麻煩的地方。當我發送請求時,完成處理程序幾乎立即觸發,沒有錯誤,並且它返回的匹配具有預期的玩家數爲零。同時,另一位玩家肯定沒有迴應這個請求。
相關代碼:
- (void) findMatch
{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = NUM_PLAYERS_PER_MATCH; //2
request.maxPlayers = NUM_PLAYERS_PER_MATCH; //2
if (nil != self.playersToInvite)
{
// we always successfully get in this if-statement
request.playersToInvite = self.playersToInvite;
request.inviteeResponseHandler = ^(NSString *playerID, GKInviteeResponse
response)
{
[self.delegate updateUIForPlayer: playerID accepted: (response ==
GKInviteeResponseAccepted)];
};
}
request.inviteMessage = @"Let's Play!";
[self.matchmaker findMatchForRequest:request
withCompletionHandler:^(GKMatch *match, NSError *error) {
if (error) {
// Print the error
NSLog(@"%@", error.localizedDescription);
}
else if (match != nil)
{
self.currentMatch = match;
self.currentMatch.delegate = self;
// All players are connected
if (match.expectedPlayerCount == 0)
{
// start match
[self startMatch];
}
[self stopLookingForPlayers];
}
}];
}