与Sprite Kit集成的游戏中心?

3

如何在Sprite Kit Xcode模板中使用Game Center或GameKit框架?由于SKView无法使用“presentModalViewController”来查看排行榜等信息,因此需要使用场景(Scenes)。

另外,在iOS 6中如何认证玩家并进行其他有趣的操作呢?

谢谢!


1
你可以使用modalViews - 我正在用它来设置,效果很好。这将让你开始,UIViewController *vc = self.view.window.rootViewController。 - DogCoffee
如果您无法使其正常工作,请发布另一个问题,我会为您编写答案。 - DogCoffee
我仍然无法在SKView内打开GKGameCentreViewController。你能帮忙吗?这是我得到的错误...应用程序因未捕获的异常“NSInvalidArgumentException”而终止,原因是:“应用程序尝试在自身上呈现模态视图控制器。呈现控制器是<GKGameCenterViewController>”。 - iDevMartin
创建一个新问题,询问如何在Sprite Kit中呈现模态视图 - 我会尽快回复。 - DogCoffee
好的,我创建了一个新问题,这是链接:https://dev59.com/AXjZa4cB1Zd3GeqPbTvc - iDevMartin
4个回答

4
您可以使用以下代码访问根视图控制器,从而使用“presentModalViewController”:

presentModalViewController

UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: gameCenterController animated: YES completion:nil];

现在你可以在任何地方访问你的ModelViewController,包括SKScenes中。我在我的最新游戏中使用了它,效果很好。
此外,我建议您使用单独的对象来控制游戏中心,如排行榜和成就,这样您就可以在下一款游戏中重复使用它。

它不起作用,所以我尝试了这个... GKGameCenterViewController *vc = [[GKGameCenterViewController alloc] init]; [vc presentViewController:vc animated:YES completion:nil]; 但仍然不起作用。 - iDevMartin

0

Swift 2.0

 func authenticateLocalPlayer() {

        let localPlayer = GKLocalPlayer.localPlayer()

        localPlayer.authenticateHandler = {  (viewController, error )  -> Void in

            if (viewController != nil) {

                let vc:UIViewController = self.view!.window!.rootViewController!
                vc.presentViewController(viewController!, animated: true, completion:nil)

            } else {


                print ("Authentication is \(GKLocalPlayer.localPlayer().authenticated) ")
                GlobalData.loggedIntoGC = true



                // do something based on the player being logged in.

全局数据Swift文件:

static var loggedIntoGC:Bool = false

在启用Game Center的场景中调用方法:例如在HUD或GameScene中。
override func didMoveToView(view: SKView)` 
    authenticateLocalPlayer()

0

这里是一个更新的本地玩家认证,但Ravindra的代码也能运行。

- (void) authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil)
        {
            //showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.
            //[self showAuthenticationDialogWhenReasonable: viewController];
        }
        else if (localPlayer.isAuthenticated)
        {
            //authenticatedPlayer: is an example method name. Create your own method that is called after the loacal player is authenticated.
            //[self authenticatedPlayer: localPlayer];
        }
        else
        {
            //[self disableGameCenter];
        }
    };
}

-1

你可以像这样进行身份验证

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
            if (error == nil)
            {
                static_setEnable( true );

                NSLog(@" Authenticate local player complete");

            }
            else
            {
                static_setEnable( false );
                NSLog(@"Authenticate local player Error: %@", [error description]);
            }
        }];
    }

1
这不是一个很好的答案,因为SpriteKit仅适用于iOS 7,而authenticateWithCompletionHandler:自iOS 6以来已被弃用。 - idmean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接