iOS游戏中心GameKit程序化邀请配对

iOS游戏中心GameKit程序化邀请配对,ios,objective-c,game-center,gamekit,gkmatchmaker,Ios,Objective C,Game Center,Gamekit,Gkmatchmaker,我正试图实现一个带有自定义UI的实时多人游戏(没有GKMatchMakerViewController)。我正在使用StartBrowsing for NearByPlayers with reachable Handler:^(NSString*playerID,BOOL reachable)查找本地玩家,然后使用GKMatchmaker singleton启动一个匹配请求(我已经启动了) 这就是我遇到麻烦的地方。当我发送一个请求时,完成处理程序几乎立即启动,没有错误,并且它返回的匹配的预期玩

我正试图实现一个带有自定义UI的实时多人游戏(没有GKMatchMakerViewController)。我正在使用StartBrowsing for NearByPlayers with reachable Handler:^(NSString*playerID,BOOL reachable)查找本地玩家,然后使用GKMatchmaker singleton启动一个匹配请求(我已经启动了)

这就是我遇到麻烦的地方。当我发送一个请求时,完成处理程序几乎立即启动,没有错误,并且它返回的匹配的预期玩家计数为零。同时,另一个玩家肯定没有回应这个请求

相关代码:

- (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
在上面的代码中,但我不知道应该包含在哪里。我在GKMatchRequestInviterResponseHandler和matchmaker finMatchForRequest:withCompletionHandler中都试过了,但都没有用。所发生的行为是媒人立即返回匹配(甚至在被邀请者被邀请之前),并且即使在被邀请者点击匹配邀请之后,也不会调用matchRequest InviterResponseHandler

有人能就此提供建议吗?多谢各位


…吉姆

我今晚的比赛刚刚开始。您需要进行更多的协商才能设置通信通道。返回给邀请者的初始匹配正在等待被邀请者响应。。。这是我的流程,只有两名玩家。以下是我的通信加速所执行的所有步骤。显然,此处不包含真正的错误处理:

首先,验证您的播放器

第二,在身份验证之后立即设置inviteHandler。大概是这样的:

[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite* acceptedInvite, NSArray *playersToInvite)
{
    if(acceptedInvite != nil)
    {
        // Get a match for the invite we obtained...
        [[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^(GKMatch *match, NSError *error)
        {
            if(match != nil)
            {
                [self disconnectMatch];
                // Record the new match...
                self.MM_gameCenterCurrentMatch = match;
                self.MM_gameCenterCurrentMatch.delegate = self;
             }
            else if(error != nil)
            {
                NSLog(@"ERROR: From matchForInvite: %@", [error description]);
            }
            else 
            {
                NSLog(@"ERROR: Unexpected return from matchForInvite...");
            }
         }];
    }
};
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state{
switch (state)
{
    case GKPlayerStateConnected:
        // Handle a new player connection.
        break;
    case GKPlayerStateDisconnected:
        // A player just disconnected.
        break;
}
if (!self.matchStarted && match.expectedPlayerCount == 0)
{
    self.matchStarted = YES;
    // Handle initial match negotiation.
    if (self.iAmHost && !self.sentInitialResponse)
    {
        self.sentInitialResponse = true;
        // Send a hello log entry
        [self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for accepting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
    }
}}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID{
NSString* actionString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
// Send the initial response after we got the initial send from the
// invitee...
if (!self.iAmHost &&!self.sentInitialResponse)
{
    self.sentInitialResponse = true;
    // Send a hello log entry
    [self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for inviting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
}
// Execute the action we were sent...
NSLog(actionString);}
第三,获取您的好友playerid列表(而不是别名)

第四,像这样设置你的请求。。。我只邀请一位朋友:

// Initialize the match request - Just targeting iOS 6 for now...
GKMatchRequest* request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = [NSArray arrayWithObject:player.playerID];
request.inviteMessage = @"Let's play!";
// This gets called when somebody accepts
request.inviteeResponseHandler = ^(NSString *playerID, GKInviteeResponse response)
{
    if (response == GKInviteeResponseAccepted)
    {
        //NSLog(@"DEBUG: Player Accepted: %@", playerID);
        // Tell the infrastructure we are don matching and will start using the match
        [[GKMatchmaker sharedMatchmaker] finishMatchmakingForMatch:self.MM_gameCenterCurrentMatch];
     }
};
第五,使用请求调用findMatchForRequest:withCompletionHandler:类似这样的函数

    [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch* match, NSError *error) {
    if (error)
    {
        NSLog(@"ERROR: Error makeMatch: %@", [error description] );
        [self disconnectMatch];
    }
    else if (match != nil)
    {
        // Record the new match and set me up as the delegate...
        self.MM_gameCenterCurrentMatch = match;
        self.MM_gameCenterCurrentMatch.delegate = self;
        // There will be no players until the players accept...
    }
}];
第六,这会将请求发送给其他玩家,如果他们接受第二步中的“inviteHandler”,则会调用该请求

第七,第二步中的“inviteHandler”获得与GKInvite的匹配

第八,调用第四步中的“inviterResponseHandler”,完成匹配

第九,从GKMatchDelegate创建一个didChangeState来处理匹配的最终完成。大概是这样的:

[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite* acceptedInvite, NSArray *playersToInvite)
{
    if(acceptedInvite != nil)
    {
        // Get a match for the invite we obtained...
        [[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^(GKMatch *match, NSError *error)
        {
            if(match != nil)
            {
                [self disconnectMatch];
                // Record the new match...
                self.MM_gameCenterCurrentMatch = match;
                self.MM_gameCenterCurrentMatch.delegate = self;
             }
            else if(error != nil)
            {
                NSLog(@"ERROR: From matchForInvite: %@", [error description]);
            }
            else 
            {
                NSLog(@"ERROR: Unexpected return from matchForInvite...");
            }
         }];
    }
};
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state{
switch (state)
{
    case GKPlayerStateConnected:
        // Handle a new player connection.
        break;
    case GKPlayerStateDisconnected:
        // A player just disconnected.
        break;
}
if (!self.matchStarted && match.expectedPlayerCount == 0)
{
    self.matchStarted = YES;
    // Handle initial match negotiation.
    if (self.iAmHost && !self.sentInitialResponse)
    {
        self.sentInitialResponse = true;
        // Send a hello log entry
        [self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for accepting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
    }
}}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID{
NSString* actionString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
// Send the initial response after we got the initial send from the
// invitee...
if (!self.iAmHost &&!self.sentInitialResponse)
{
    self.sentInitialResponse = true;
    // Send a hello log entry
    [self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for inviting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
}
// Execute the action we were sent...
NSLog(actionString);}
第十,这是我的留言:

- (void) sendMessage:(NSString*)action toPlayersInMatch:(NSArray*) playerIds{   
NSError* err = nil;
if (![self.MM_gameCenterCurrentMatch sendData:[action dataUsingEncoding:NSUTF8StringEncoding] toPlayers:playerIds withDataMode:GKMatchSendDataReliable error:&err])
{
    if (err != nil)
    {
        NSLog(@"ERROR: Could not send action to players (%@): %@ (%d) - '%@'" ,[playersInMatch componentsJoinedByString:@","],[err localizedDescription],[err code], action);
    }
    else
    {
        NSLog(@"ERROR: Could not send action to players (%@): null error - '%@'",[playersInMatch componentsJoinedByString:@","], action);
    }
}
else
{
    NSLog(@"DEBUG: Message sent to players (%@) - '%@'",[playersInMatch componentsJoinedByString:@","], action);
}}
第十一,从GKMatchDelegate创建didReceiveData,如下所示:

[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite* acceptedInvite, NSArray *playersToInvite)
{
    if(acceptedInvite != nil)
    {
        // Get a match for the invite we obtained...
        [[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^(GKMatch *match, NSError *error)
        {
            if(match != nil)
            {
                [self disconnectMatch];
                // Record the new match...
                self.MM_gameCenterCurrentMatch = match;
                self.MM_gameCenterCurrentMatch.delegate = self;
             }
            else if(error != nil)
            {
                NSLog(@"ERROR: From matchForInvite: %@", [error description]);
            }
            else 
            {
                NSLog(@"ERROR: Unexpected return from matchForInvite...");
            }
         }];
    }
};
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state{
switch (state)
{
    case GKPlayerStateConnected:
        // Handle a new player connection.
        break;
    case GKPlayerStateDisconnected:
        // A player just disconnected.
        break;
}
if (!self.matchStarted && match.expectedPlayerCount == 0)
{
    self.matchStarted = YES;
    // Handle initial match negotiation.
    if (self.iAmHost && !self.sentInitialResponse)
    {
        self.sentInitialResponse = true;
        // Send a hello log entry
        [self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for accepting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
    }
}}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID{
NSString* actionString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
// Send the initial response after we got the initial send from the
// invitee...
if (!self.iAmHost &&!self.sentInitialResponse)
{
    self.sentInitialResponse = true;
    // Send a hello log entry
    [self sendMessage: [NSString stringWithFormat:@"Message from friend, 'Hello, thanks for inviting, you have connected with %@'", self.MM_gameCenterLocalPlayer.alias] toPlayersInMatch: [NSArray arrayWithObject:playerID]];
}
// Execute the action we were sent...
NSLog(actionString);}

第十二。。。好了,现在你有了沟通渠道并开始运作。。。想做什么就做什么…

你是如何开始为近距离的玩家划船的,并且有一个可接触的处理程序来工作的?我从来没有收到过它的任何回调??你知道在iOS9中这样做的正确方法吗?我刚才问了一个类似的问题: