IOS游戏中心GKLocalPlayerListener [英] IOS Game Center GKLocalPlayerListener

查看:143
本文介绍了IOS游戏中心GKLocalPlayerListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在回合制游戏中实现一个事件监听器,这样玩家可以在轮到他或者被朋友邀请时接收。在IOS 7中不推荐使用GKTurnBasedEventHandler,我在文档中读到了我应该使用的GKLocalPlayerListener;但这是它的延伸。是否有人使用过它,因为任何地方都没有信息。

I was trying to implement an event listener in a turn based game so a player can receive when his turn is active or when he is invited by a friend. GKTurnBasedEventHandler is deprecated in IOS 7 and i read in the documentation that i should use GKLocalPlayerListener; but that's the extend of it. Is there someone who used it already, because there is no info anywhere.

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

This is what i tried before, and it does not work.

- (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");
}


推荐答案

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

Here is some code that I use in order to register 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,这样您就可以通过检查是否可以改进此代码你已经注册了。

The documentation states that you should only register for an GKLocalPlayerEventListener once so you could improve this code by checking if you've already registered.

请注意,iOS 6中不推荐使用 authenticateWithCompletionHandler ,他们建议将authenticateHandler属性设置为我在上面做了。

Note that authenticateWithCompletionHandler is deprecated in iOS 6 and they recommend setting the authenticateHandler property like I did above.

这篇关于IOS游戏中心GKLocalPlayerListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆