GKTurnBasedEventHandler delegate is not receiving any messages

僤鯓⒐⒋嵵緔 提交于 2019-12-06 13:34:17

My current belief is that the GameCenter Sandbox is at fault, since it seems like many people have had problems with it. To be able to test my code, I actually wrote some code to poll GameCenter and look for changes.

WARNING This is DUMB. I have it enabled in DEBUG mode only, simply so that I can test my handleTurnEventForMatch code. That said... it works around the problem.

This code can go into the same class that provides your delegate methods. You'll need to do some obvious logic modifications. You should call onMultiplayerGameStarted and onEndedMultiplayerTurn in the appropriate places from your gameplay logic.

#if GAMEKIT_TURN_POLLING

NSMutableDictionary *_wasLocalPlayersTurnMap = nil;

- (void)pollGameCenter {
  if(!_wasLocalPlayersTurnMap) {
    _wasLocalPlayersTurnMap = [NSMutableDictionary new];
  }
  [AMGameData loadGames:^(NSArray *games) {
    NSInteger validGameCount = 0;
    for(AMGameData *gameData in games) {
      if(gameData.isSinglePlayer) {
        continue;
      }
      if(gameData.gameState != AMGameStatePlaying) {
        continue;
      }
      validGameCount++;
      if([_wasLocalPlayersTurnMap[gameData.name] boolValue]) {
        continue;
      }
      if(!gameData.isLocalPlayersTurn) {
        _wasLocalPlayersTurnMap[gameData.name] = @(NO);
        continue;
      }
      // Hey, it's now our turn!
      _wasLocalPlayersTurnMap[gameData.name] = @(YES);
      [self handleTurnEventForMatch:gameData.match didBecomeActive:NO];
    }
    if(validGameCount) {
      // Need to do this again later...
      [self delayedPollGameCenter];
    }
  }];
}

- (void)delayedPollGameCenter {
  [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(pollGameCenter) object:nil];
  [self performSelector:@selector(pollGameCenter) withObject:nil afterDelay:10];
}

- (void)onMultiplayerGameStarted {
  [self delayedPollGameCenter];
}

- (void)onEndedMultiplayerTurn:(AMGameData*)gameData {
  _wasLocalPlayersTurnMap[gameData.name] = @(NO);
  [self delayedPollGameCenter];
}

#else
- (void)onMultiplayerGameStarted{}
- (void)onEndedMultiplayerTurn:(AMGameData*)gameData {}
#endif

I'm having the same problems as well. I'm getting notifications around ~ 25% of time and I can't test properly the app.

According to the documentation, GKTurnBasedEventHandler is deprecated on iOS7. If this is true - could this be the reason for these issues?

https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKTurnBasedEventHandler_Ref/Reference/Reference.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!