Mac*_*ret 9 ios game-center gkturnbasedmatch
我试图在回合制游戏中实现一个事件监听器,这样玩家可以在轮到他或者被朋友邀请时接收.在IOS 7中不推荐使用GKTurnBasedEventHandler,我在文档中读到了我应该使用的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");
}
Run Code Online (Sandbox Code Playgroud)
这是我用来注册 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]);
}
};
Run Code Online (Sandbox Code Playgroud)
该文档指出,您应该只注册一次 GKLocalPlayerEventListener,这样您就可以通过检查您是否已经注册来改进此代码。
请注意,这authenticateWithCompletionHandler在 iOS 6 中已被弃用,他们建议像我上面那样设置authenticateHandler 属性。