簡體   English   中英

iOS游戲中心GameKit程序化邀請匹配

[英]iOS Game Center GameKit Programmatic Invite Matchmaking

我正在嘗試使用自定義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 Programmatic Matchmaking )中了解到,我需要包含這個:

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

在上面的代碼,但我不知道應該包括在哪里。 我已經嘗試了GKMatchRequest inviteeResponseHandler,並在matchmaker finMatchForRequest:withCompletionHandler中無濟於事。 發生的行為是匹配器立即返回匹配(甚至在被邀請者被邀請之前)並且即使在被邀請者點擊匹配邀請之后也從未調用matchRequest inviteeResponseHandler。

有人可以就此提出建議嗎? 謝謝。

...吉姆

我今晚剛剛開始研究這個游戲。 為了獲得通信通道設置,您需要進行更多的協商。 返回給邀請者的初始匹配正在等待被邀請者回復...這是我的過程只有兩個玩家。 以下是我的通信啟動所執行的所有步驟。 顯然,此處不包含真正的錯誤處理:

首先,驗證您的播放器

第二,在認證設置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);
}}

第十一,從GKMatchDelegate創建一個didReceiveData,如下所示:

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

第十二......好了,現在你已經開始運行了溝通渠道......做你想做的事......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM