![](/img/trans.png)
[英]Game Center Quickmatch: randomly matching opponents (GKTurnBasedMatch)
[英]Game Center Matchmaking GKTurnBasedMatch has significant lag (~1 min)
我正在通過游戲中心實現多人模式的回合制游戲。 我有2個設備(1個ipad,1個iphone)在沙盒模式下進行測試,這些設備工作正常,但最近它開始在汽車配對過程中掙扎。 在我從一個用戶發送第一個回合后,另一個設備不會立即識別該游戲,而是打開自己的新游戲。 之前它能夠立即發現在其他設備上開始的游戲,並且配對相當簡單。 而且我不記得更改與配對相關的任何部分( NSCoding
, GKTurnBasedEventHandler
, GKTurnBasedMatchmakerViewControllerDelegate
委托方法等)。
現在我從一個設備發送第一個轉彎,需要等待大約1分鍾,以便其他設備可以成功連接到該游戲。 連接發生后,endTurnWithMatchData調用沒有任何問題,它可以在1-2秒內發送和接收數據。 但如果用戶開始新游戲並且必須等待1分鍾以便其他用戶可以連接到他的游戲,那么它將不是一個好的用戶體驗。 有沒有人在汽車配對過程中遇到重大延遲? 我還沒有實現邀請,所以我無法檢查。 我與NSKeyedArchiver存檔的matchdata似乎相當大,3396字節,即使對於幾乎沒有數據的新游戲。 以下是我的代碼的相關部分:
GameOptionsViewController
:
- (void)turnBasedMatchmakerViewControllerWasCancelled:(GKTurnBasedMatchmakerViewController *)viewController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFailWithError:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFindMatch:(GKTurnBasedMatch *)match
{
[self dismissViewControllerAnimated:NO completion:nil];
self.gcMatch = match;
[self performSegueWithIdentifier:@"GameMultiplayer" sender:self];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"GameMultiplayer"])
{
GameViewController *GameVC = (GameViewController *)segue.destinationViewController;
[GameVC setGameMode:GAMEMODE_MULTIPLAYER_SAMEDEVICE];
//Multiplayer game it is
if(self.gcMatch != nil)
{
[GameVC setGameMode:GAMEMODE_MULTIPLAYER_GAMECENTER];
GameVC.gcMatchDelegate = self;
GameVC.gcMatch = self.gcMatch;
NSLog(@"Game OVC Segue: Match ID | %@", self.gcMatch.matchID);
}
}
else
{
...
}
}
GameViewController
:
//This method is called according to user actions
//It's the only method I use to send data to other participant
-(void) sendCurrentGameDataWithNewTurn:(BOOL) newTurn
{
NSLog(@"Sending game data current participant : %@", gcMatch.currentParticipant.playerID);
//Update match data if it is corrupted anyhow
if (gcMatch.currentParticipant == nil)
{
[GKTurnBasedMatch loadMatchWithID:gcMatch.matchID withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
{
if (error != nil)
{
NSLog(@"Error :%@", error);
return ;
}
[self sendCurrentGameDataWithNewTurn:newTurn];
}];
}
else
{
NSData *matchData = [NSKeyedArchiver archivedDataWithRootObject:game];
//Game advances to new player, buttons are disabled
if(newTurn)
{
NSLog(@"SENDING NEW TURN");
NSUInteger currentIndex = [gcMatch.participants
indexOfObject:gcMatch.currentParticipant];
GKTurnBasedParticipant *nextParticipant;
nextParticipant = [gcMatch.participants objectAtIndex:
((currentIndex + 1) % [gcMatch.participants count])];
[gcMatch endTurnWithNextParticipants:[NSArray arrayWithObject:nextParticipant] turnTimeout:GC_TURN_TIMEOUT matchData:matchData completionHandler:^(NSError *error) {
NSLog(@"Sent");
if (error) {
NSLog(@"SNT - %@", error);
}
}];
}
else
{
NSLog(@"ONLY UPDATING DATA");
[gcMatch saveCurrentTurnWithMatchData:matchData completionHandler:^(NSError *error) {
NSLog(@"Sent");
if (error) {
NSLog(@"OUD - %@", error);
}
}];
}
}
}
-(void) updateGameDataWithGCMatch
{
//Update whole game data
self.game = [NSKeyedUnarchiver unarchiveObjectWithData:self.gcMatch.matchData];
//Update game ui
...
}
-(void) handleTurnEventForMatch:(GKTurnBasedMatch *)match didBecomeActive:(BOOL)didBecomeActive
{
//Check if I got data for the currently active match that options vc forwarded me here, if not do some debug print and return
if(![self.gcMatch.matchID isEqual:match.matchID])
{
//For debugging reasons I skip if i get info for any previous match (other player quit etc)
NSLog(@"GCMatch matchID: %@ match matchID: %@",self.gcMatch.matchID,match.matchID);
return;
}
NSLog(@"Turn event handle");
self.gcMatch = match;
if([match.currentParticipant.playerID isEqualToString: [GKLocalPlayer localPlayer].playerID ])
{
//Disable field buttons
[self setFieldButtonsEnabled:TRUE];
[self turnChangeAnimationFromLeftToRight:FALSE];
}
[self updateGameDataWithGCMatch];
}
至於你的問題:
我自己在游戲中心的配對上做了相當多的調整,並經常經歷滯后,這已被證明不是由我的網站引起的,而是由蘋果游戲中心服務器造成的。
至於其他指導:
據我所知,您目前在設備上進行配對的方法是:
看看是否有匹配我可以連接 - >如果YES請求gamedata並連接到匹配ELSE開始你自己的匹配並廣播matchdata
根據我的經驗,最好先練習matchrequestbroadcasts,等到找到第二個玩家,定義服務器設備(例如通過較低的游戲中心名校驗和),然后在該設備上啟動游戲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.