Game Center沙盒问题



我遇到了一个非常古老的问题,这个问题在这里已经讨论了很多次。尽管我花了很多时间讨论这个问题,但我没有找到可以接受的解决方案,所以我决定再次提出这个问题。

所以,问题来了。我正在尝试测试回合制比赛。我用了两个真正的设备。我打开了第一个设备,匹配数据更新没有错误(我肯定知道),但有时第二个设备没有收到任何通知,第一个设备似乎仍在转动。有时它会像预期的那样工作。

换句话说,有时不调用player(_:receivedTurnEventFor:didBecomeActive)方法。但如果我在第二台设备上关闭应用程序,重新打开它并加入现有的匹配,一切都会很好。据我所知,这是众所周知的游戏中心沙盒问题,但当我试图测试应用程序时,它让我抓狂。有人知道如何变通吗?或者,也许有一个最佳实践,如何使用这种奇怪的沙箱行为来生活和测试应用程序?

更新。Thunk提出的方法是一种解决方案。我用Swift重写了它,并根据我的游戏逻辑进行了修改。首先,我定义了全局变量var gcBugTimer: Timer

endTurn(withNextParticipants:turnTimeOut:match:completionHan‌​dler:)完成处理程序中:

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];
}
}];
}

最新更新