簡體   English   中英

iOS GameCenter Matchmaker無法正常工作

[英]iOS GameCenter Matchmaker not working

我正在嘗試使用媒人制作自定義媒人視圖。 以下代碼用於查找匹配項。

當我在具有不同Game Center帳戶的兩個不同設備上運行此游戲時,兩者都將獲得匹配,但沒有一個將連接到該匹配。 它們只會陷入無限循環的while循環中,永遠不會消失。 我錯過了什么嗎,您是否需要打電話才能真正連接到比賽?

- (void) findMatch{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = nil;
NSLog(@"Start searching!");

[matchmaker findMatchForRequest:request
               withCompletionHandler:^(GKMatch *match, NSError *error)
 {
     if (error) {
         // Print the error
         NSLog(@"%@", error.localizedDescription);
     }
     else if (match != nil)
     {
         curMatch = match;
         curMatch.delegate = self;


         NSLog(@"Expected: %i", match.expectedPlayerCount);

         while (match.expectedPlayerCount != 0){
             NSLog(@"PLayers: %i", curMatch.playerIDs.count);
         }
         NSLog(@"Start match!");
     }
 }];

您不應該使用while循環來等待ExpectedPlayerCount達到0,而是實現GKMatchDelegate方法:

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
    if (!self.matchStarted && match.expectedPlayerCount == 0) {
        self.matchStarted = YES;
        //Now you should start your match.
    }
}

暫無
暫無

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

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