[英]Game Center Sandbox issue
我遇到了一個很老的問題,這里已經討論過很多次了。 盡管多次討論這個問題,但我沒有找到可接受的解決方案,所以我決定再次提出這個問題。
所以,問題。 我正在嘗試測試回合制比賽。 為此,我使用了兩個真實設備。 我打開第一個設備,並且匹配數據更新沒有錯誤(我肯定知道),但有時第二個設備沒有收到任何通知,而且第一個設備似乎仍在轉動。 有時它會按預期工作。
換句話說,有時不會調用player(_:receivedTurnEventFor:didBecomeActive)
方法。 但是,如果我在第二台設備上關閉該應用程序,請重新打開它並加入現有匹配,一切正常。 據我所知,這是眾所周知的 Game Center Sandbox 問題,但在我嘗試測試應用程序時,這讓我發瘋。 有誰知道如何解決的方法? 或者也許有最佳實踐如何使用這種奇怪的沙箱行為來運行和測試應用程序?
更新。 Thunk 提出的方法是一個解決方案。 我用 Swift 重寫並修改為符合我的游戲邏輯。 首先,我定義了全局變量var gcBugTimer: Timer
在endTurn(withNextParticipants:turnTimeOut:match:completionHandler:)
完成處理程序中:
let interval = 3.0
self.gcBugTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(self.isMatchActive), userInfo: nil, repeats: true)
self.gcBugTimer.tolerance = 1.0
上面的代碼也應該被調用,以防當一個玩家正在為新的比賽和一個回合中的其他玩家感到高興時。
然后定時器方法:
func isMatchActive() {
// currentMatch - global variable contains information about current match
GKTurnBasedMatch.load(withID: currentMatch.matchID!) { (match, error) in
if match != nil {
let participant = match?.currentParticipant
let localPlayer = GKLocalPlayer.localPlayer()
if localPlayer.playerID == participant?.player?.playerID {
self.player(localPlayer, receivedTurnEventFor: match!, didBecomeActive: false)
}
} else {
print(error?.localizedDescription ?? "")
}
}
}
我在player(_:receivedTurnEventFor:didBecomeActive)
開頭添加以下代碼:
if gcBugTimer != nil && gcBugTimer.isValid {
gcBugTimer.invalidate()
}
我發現唯一可靠的解決方案是在等待輪到我時手動重新檢查我的狀態。 在endTurnWithNextParticipants
的完成處理程序中,我設置了一個計時器以連續重新加載匹配數據。 我檢查了localPlayer
是否已成為活動玩家。 如果是這樣,那么我自己調用了receivedTurnForEvent
,否則,我重復了計時器。 像這樣:
在endTurnWithNextParticipants
完成處理程序中:
float dTime = 60.0; //messages sometimes fail in IOS8.4
if (SYSTEM_VERSION_EQUAL_TO(@"8.3") )
{
dTime = 5.0; //messages always fail in IOS8.3
}
IOS8BugTimer = [NSTimer scheduledTimerWithTimeInterval:dTime
target:gameKitHelper
selector:@selector(isMatchActive:)
userInfo:theMatch.matchID
repeats:NO];
在 gameKitHelper:isMatchActive 中:
-(void)isMatchActive:(NSTimer *)timer
{
NSString *matchID = (NSString *)timer.userInfo;
[GKTurnBasedMatch loadMatchWithID:matchID withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
GKTurnBasedParticipant *currentParticipant = match.currentParticipant;
if ([localPlayer.playerID isEqualToString:currentParticipant.player.playerID])
{
//we have become active. Call the event handler like it's supposed to be called
[self player:localPlayer receivedTurnEventForMatch:match didBecomeActive:false];
}
else
{
//we are still waiting to become active. Check back soon
float dTime = 60.0;
if (SYSTEM_VERSION_EQUAL_TO(@"8.3") )
{
dTime = 5.0;
}
gameController.IOS8BugTimer = [NSTimer scheduledTimerWithTimeInterval:dTime
target:self
selector:@selector(isMatchActive:)
userInfo:matchID
repeats:NO];
}
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.