10

我试图在基于回合的游戏中实现一个事件监听器,这样玩家就可以在轮到他或他被朋友邀请时接收。GKTurnBasedEventHandler 在 IOS 7 中已弃用,我在文档中读到我应该使用 GKLocalPlayerListener;但这就是它的延伸。有没有人用过,因为到处都没有信息。

这是我之前尝试过的,它不起作用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer authenticateWithCompletionHandler:^(NSError *error)
     {
         if (localPlayer.isAuthenticated)
         { 
             GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
             [localPlayer registerListener:self];
         }
     }];

    return YES;
}

-(void)handleInviteFromGameCenter:(NSArray *)playersToInvite
{
    NSLog(@"test");
}

- (void)player:(GKPlayer *)player receivedTurnEventForMatch:(GKTurnBasedMatch *)match didBecomeActive:(BOOL)didBecomeActive
{
    NSLog(@"test");
}
4

3 回答 3

2

这是我用来注册 GKLocalPlayerListener 的一些代码

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
   if (viewController) {
         [authenticateFromViewController presentViewController:viewController animated:YES completion:^{
          [localPlayer registerListener:self];
          NSLog(@"Authenticated. Registering Turn Based Events listener");
        }];
  } else if (localPlayer.authenticated) {
         [localPlayer registerListener:self];
         NSLog(@"User Already Authenticated. Registering Turn Based Events listener");
  } else {
         NSLog(@"Unable to Authenticate with Game Center: %@", [error localizedDescription]);
  }
};

该文档指出,您应该只注册一次 GKLocalPlayerEventListener,因此您可以通过检查您是否已经注册来改进此代码。

请注意,这authenticateWithCompletionHandler在 iOS 6 中已弃用,他们建议像我在上面所做的那样设置 authenticateHandler 属性。

于 2013-11-20T00:03:50.600 回答
1

我相信你在那里。只是这次做几件事。确保在添加侦听器之前也不要添加多个侦听器,只需取消注册所有侦听器即可。

我确保我在整个项目中只这样做了一次,但我多次获得本地播放器。

-(void) onLocalPlayerAuthChanged:(GKLocalPlayer*)authPlayer {

    [authPlayer unregisterAllListeners];
    [authPlayer registerListener:_Whatever_];

}
于 2013-11-27T06:43:39.283 回答
1

我可能有点晚了,但希望它会帮助那里的人......

这就是我所做的。根据Apple 的文档,我创建了 [my] 自己的方法,该方法在适合 [my] 应用程序时显示身份验证视图。

    - (void)authenticateLocalUser
    {
        if ([GKLocalPlayer localPlayer].authenticated == NO) {
            __weak typeof(self) weakSelf = self;
            __weak GKLocalPlayer *weakPlayer = [GKLocalPlayer localPlayer];

            weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
                if (viewController != nil) {
                    [weakSelf showAuthenticationDialogWhenReasonable:viewController];
                } else if (weakPlayer.isAuthenticated) {
                    // Player has been authenticated!
                    [weakPlayer registerListener:weakSelf];
                } else {
                    // Should disable Game Center?
                }
            };

        } else {
            // Already authenticated
            [[GKLocalPlayer localPlayer] registerListener:self];
        }
    }


    -(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller
    {
        [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil];
    }

这段代码在一个单例助手类中,如果你在自己的类中有它可能会被简化。

于 2014-03-19T18:54:41.940 回答