iOS Gamecenter程序配对 [英] iOS Gamecenter Programmatic Matchmaking
问题描述
我正在尝试使用自定义UI(无GKMatchMakerViewController
)实现实时多人游戏.我正在使用startBrowsingForNearbyPlayersWithReachableHandler:
^(NSString *playerID, BOOL reachable)
查找本地球员,然后使用GKMatchmaker
单身人士(我已经开始)发起比赛请求.
I'm trying to implement a real-time multiplayer game with a custom UI (no GKMatchMakerViewController
). I'm using startBrowsingForNearbyPlayersWithReachableHandler:
^(NSString *playerID, BOOL reachable)
to find a local player, and then initiating a match request with the GKMatchmaker
singleton (which I have already initiated).
这是我遇到麻烦的地方.当我发送请求时,完成处理程序几乎立即触发,没有错误,并且返回的比赛预期玩家计数为零.同时,其他玩家肯定没有响应该请求.
Here's where I'm having trouble. When I send a request, the completion handler fires almost immediately, without an error, and the match it returns has an expected player count of zero. Meanwhile, the other player definitely has not responded to the request.
相关代码:
- (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];
}
}];
}
推荐答案
想通了!我需要在邀请处理程序中调用- (void)matchForInvite:(GKInvite *)invite completionHandler:(void (^)(GKMatch *match, NSError *error))completionHandler
,以便两个玩家都具有相同的比赛数据.
Figured it out! I needed to call - (void)matchForInvite:(GKInvite *)invite completionHandler:(void (^)(GKMatch *match, NSError *error))completionHandler
in my invitation handler so that both players have the same match data.
这篇关于iOS Gamecenter程序配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!