2011-01-25 62 views
5

我試圖使用遊戲中心後稱爲:多玩家遊戲中心:匹配委託的未找到匹配

截至目前,玩家要進行身份驗證遊戲中心,它們可以發送/讀取的分數,並acheivements。 對於多人遊戲功能,我嘗試了兩種方法: - 使用遊戲中心界面來查找比賽。 - 以編程方式查找匹配項。

對於這兩種方式,我有以下問題:匹配委託的匹配:player:didChangeState:方法不被調用。 在蘋果文檔中,聲明如果一個玩家連接或斷開連接,則會調用此代理。

在我的情況下,這個委託永遠不會被調用。我認爲我錯過了一步。 這裏是在執行我的委託之後(如apple doc中指定的)。

- (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. 
    } 
} 

並且還找到匹配的代碼。

-(void) findProgrammaticMatch 
{ 
    GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
    request.minPlayers = 2; 
    request.maxPlayers = 2; 

    [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request 
           withCompletionHandler:^(GKMatch *FoundMatch, NSError *error) 
    { 
    if (error) 
    { 
     // Process the error. 
     StatusLabel.text = @"Match Not Found"; 
    } 
    else if (FoundMatch != nil) 
    { 
     MultiPlayerMatch = FoundMatch; // Use a retaining property to retain the match. 
     StatusLabel.text = @"Match Found"; 
     MultiPlayerMatch.delegate = self; // start! 
     // Start the match. 
     // Start the game using the match. 
     [self StartMatch]; 
    } 
    }]; 
} 

感謝您的幫助。

回答

1

它一直在工作。唯一的區別是...當您使用邀​​請事件「didChangeState」不會被調用。您無需通知即可連接,並且您可以開始接收數據。我從來沒有嘗試發送/接收數據,因爲我首先期待事件,但我確實錯誤地發過一次,並且它工作。

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *) match {  
    //Dismiss window 
    [self dismissModalViewControllerAnimated:YES]; 

    //Retain match 
    self.myMatch = match; 

    //Delegate 
    myMatch.delegate = self; 


    //Flag 
    matchStarted = TRUE; 

    //Other stuff 
} 

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state { 
    //This code gets called only on auto-match 
} 

上述代碼按預期工作。

+0

我不相信這是真的。我剛剛測試過自動匹配,didChangeState從不會觸發。只有didFindMatch。我認爲didChangeState可能只有在玩家進入GKPlayerStateUnknown然後回來,或者他們被添加到進行中的比賽時纔會發生。 – 2014-12-02 09:23:51

0

我認爲didChangeState:GKPlayerStateConnected可能只發生在玩家被GKPlayerStateUnknown返回,或者如果他們被添加/邀請回到正在進行的比賽中。