我试图在回合制游戏中实现一个事件监听器,这样玩家可以在轮到他或者被朋友邀请时接收。在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");
}
答案 0 :(得分: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,这样您就可以通过检查是否已经注册来改进此代码。
请注意,在iOS 6中不推荐使用authenticateWithCompletionHandler
,他们建议像我上面那样设置authenticateHandler属性。
答案 1 :(得分:1)
我相信你在那里。就在这个时候做几件事。在添加侦听器之前,请确保不要添加多个侦听器,只需注销所有侦听器。
我确保我在整个项目中只做了一次,但我多次得到本地玩家。
-(void) onLocalPlayerAuthChanged:(GKLocalPlayer*)authPlayer {
[authPlayer unregisterAllListeners];
[authPlayer registerListener:_Whatever_];
}
答案 2 :(得分:1)
我可能会有点迟到,但希望它可以帮助那些人......
这就是我的工作。根据{{3}}我创建[my]自己的方法,在适合[my] app时显示身份验证视图。
- (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];
}
此代码位于单例辅助类中,如果您在自己的类中使用它,则可能会简化。