Swift-集成GameCenter以使用排行榜 [英] Swift - Integrate GameCenter to use leaderboards

查看:98
本文介绍了Swift-集成GameCenter以使用排行榜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Swift制作游戏.我希望能够使用GameCenter发布用户的分数,以便可以看到我所有用户的分数.但是,我花了整整一天的时间试图弄清楚该如何做,但是我没有找到任何有用的说明.

我对iOS编程和Swift还是很陌生,关于这个主题的信息很少,这些都是用Objective-C编写的.

任何人都可以帮助我将GameCenter集成到我的应用中,以便我可以将用户分数发布到排行榜上,以供人们查看吗?

我已经在iTunesConnect上创建了GameCenter排行榜.

我已尝试按照本教程进行操作: http://www.appcoda.com/ios- game-kit-framework/并将其转换为Swift.我已将其转换为

-(void)authenticateLocalPlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {
            [self presentViewController:viewController animated:YES completion:nil];
        }
        else{
            if ([GKLocalPlayer localPlayer].authenticated) {
                _gameCenterEnabled = YES;

                // Get the default leaderboard identifier.
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    }
                    else{
                        _leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            }

            else {
                _gameCenterEnabled = NO;
            }
        }
    };
}

对此:

func authenticateLocalPlayer() {
    var localPlayer : GKLocalPlayer!
    localPlayer.authenticateHandler = {(viewController : MenuViewController!, error : NSError!) -> Void in
        if viewController != nil {
            self.presentViewController(viewController, animated: true, completion: nil)
        } else {
            if localPlayer.authenticated {
                self.gameCenterEnabled = true

                localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifier : String!, error : NSError!) -> Void in
                    if error != nil {
                        println(error.localizedDescription)
                    } else {
                        self.leaderboardIdentifier = leaderboardIdentifier
                    }
                })

            } else {
                self.gameCenterEnabled = false
            }
        }
    }
}

但它在此行崩溃:

localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in

错误消息是:

致命错误:解开可选值时意外发现nil

我不敢相信这有多难!

解决方案

您的特定问题与Game Center无关,并且正在发生,因为您有var localPlayer : GKLocalPlayer!行.

您要声明一个隐式解包的可选内容,然后立即使用它.当您尝试设置localPlayer.authenticateHandler时,在下一行中它的值为nil.

相反,您应该像这样实例化GKLocalPlayer:

var localPlayer = GKLocalPlayer()

请注意,Game Center和Swift当前存在问题.您的代码将正常工作,但localPlayer.authenticated永远不会设置为true.在此跟踪此问题:

http://www.openradar.me/17825348

信用至: http://www.stuarticus .net/blog/2014/7/game-center-authentication-and-swift 提交雷达票.

I am making a game in Swift. I want to be able to post the users' score using GameCenter, so that scores from all my users' can be seen. However, I have spent the past day trying to figure out how to do this, but I haven't found any helpful instructions.

I am pretty new to iOS programming, and Swift, and of the very little amount of information on this subject, it's all written in Objective-C.

Can anyone help me integrate GameCenter into my app, so that I can post users scores to the leaderboards for people to see?

EDIT: I have already created a GameCenter leaderboard on iTunesConnect.

EDIT 2: I have tried following this tutorial: http://www.appcoda.com/ios-game-kit-framework/ and converting it to Swift. I have converted this:

-(void)authenticateLocalPlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {
            [self presentViewController:viewController animated:YES completion:nil];
        }
        else{
            if ([GKLocalPlayer localPlayer].authenticated) {
                _gameCenterEnabled = YES;

                // Get the default leaderboard identifier.
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    }
                    else{
                        _leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            }

            else {
                _gameCenterEnabled = NO;
            }
        }
    };
}

into this:

func authenticateLocalPlayer() {
    var localPlayer : GKLocalPlayer!
    localPlayer.authenticateHandler = {(viewController : MenuViewController!, error : NSError!) -> Void in
        if viewController != nil {
            self.presentViewController(viewController, animated: true, completion: nil)
        } else {
            if localPlayer.authenticated {
                self.gameCenterEnabled = true

                localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifier : String!, error : NSError!) -> Void in
                    if error != nil {
                        println(error.localizedDescription)
                    } else {
                        self.leaderboardIdentifier = leaderboardIdentifier
                    }
                })

            } else {
                self.gameCenterEnabled = false
            }
        }
    }
}

but it crashes on this line:

localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in

The Error message is:

fatal error: unexpectedly found nil while unwrapping an Optional value

I can't believe how hard this is!

解决方案

Your specific issue has nothing to do with Game Center and is happening because you have the line var localPlayer : GKLocalPlayer!.

You're declaring an implicitly unwrapped optional and then using it right away. It's value is nil in the subsequent line when you try to set localPlayer.authenticateHandler.

Instead you should instantiate GKLocalPlayer like so:

var localPlayer = GKLocalPlayer()

Note that there are currently issues with Game Center and Swift. Your code is going to work but localPlayer.authenticated never gets set to true. This issue is tracked here:

http://www.openradar.me/17825348

Credit to: http://www.stuarticus.net/blog/2014/7/game-center-authentication-and-swift for filing the radar ticket.

这篇关于Swift-集成GameCenter以使用排行榜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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