的iOS游戏中心的GameKit编程邀请对接会(iOS Game Center GameKit Pr

2019-08-18 00:19发布

我试图实现实时多人游戏的自定义UI(无GKMatchMakerViewController)。 我使用startBrowsingForNearbyPlayersWithReachableHandler:^(的NSString * playerID,BOOL可达)找到当地的球员,然后启动与GKMatchmaker单(我已经开始)的匹配要求。

这里就是我在遇到麻烦。 当我发送一个请求,完成处理器几乎立即触发,没有一个错误,并返回匹配具有零期望球员计数。 与此同时,其他球员绝对没有回应请求

相关的代码:

- (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];
      }
  }];
}

我从以前的问题(知道iOS版的GameCenter编程对接会 ),我需要包括这样的:

- (void)matchForInvite:(GKInvite *)invite completionHandler:(void (^)(GKMatch *match, NSError *error))completionHandler

在上面的代码,但我不知道在哪里应该包括在内。 我已经尝试过了两个GKMatchRequest inviteeResponseHandler,并在媒婆finMatchForRequest:withCompletionHandler无济于事。 出现这种情况的行为是媒人立刻返回匹配(受邀者已受邀甚至更早),甚至后被邀请者水龙头比赛邀请matchRequest inviteeResponseHandler永远不会被调用。

有人可以在此建言? 谢谢。

...吉姆

Answer 1:

我刚刚得到了今晚我的比赛这方面的工作。 还有就是你需要做的就是通信信道建立更多的谈判筹码。 回到邀请最初的比赛在等待被邀请人回应......这是我的过程,只有两名球员。 这里是我所有的通信自旋向上的执行步骤。 很显然,这里包括没有真正的错误处理:

首先,验证您的播放器

二,右后验证设置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...");
            }
         }];
    }
};

第三,让您的朋友playerIds(非别名)的列表。

四,设置您GKMatchRequest这样的事情...我只邀请一个朋友:

// 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!

第八,“inviteeResponseHandler”从第四步被调用它完成了比赛!

第九,创建一个GKMatchDelegate到didChangeState处理比赛的最后定稿。 事情是这样的:

- (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]];
    }
}}

十五,这是我的sendMessage:

- (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);
}}

十一,创建一个didReceiveData从GKMatchDelegate是这样的:

- (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);}

第十二......那么现在你有沟通渠道和运行...做你想做...



文章来源: iOS Game Center GameKit Programmatic Invite Matchmaking