xcode Swift - 集成 GameCenter 以使用排行榜
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25291731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - Integrate GameCenter to use leaderboards
提问by user2397282
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.
我正在用 Swift 制作游戏。我希望能够使用 GameCenter 发布用户的分数,以便可以看到我所有用户的分数。但是,过去一天我一直在试图弄清楚如何做到这一点,但我没有找到任何有用的说明。
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.
我是 iOS 编程和 Swift 的新手,关于这个主题的信息很少,都是用 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?
谁能帮我将 GameCenter 集成到我的应用程序中,以便我可以将用户分数发布到排行榜上供人们查看?
EDIT: I have already created a GameCenter leaderboard on iTunesConnect.
编辑:我已经在 iTunesConnect 上创建了一个 GameCenter 排行榜。
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:
编辑 2:我尝试按照本教程进行操作: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;
}
}
};
}
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
致命错误:在展开可选值时意外发现 nil
I can't believe how hard this is!
我不敢相信这有多难!
回答by Austen Chongpison
Your specific issue has nothing to do with Game Center and is happening because you have the line var localPlayer : GKLocalPlayer!
.
您的具体问题与 Game Center 无关,并且正在发生,因为您有线路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
.
您正在声明一个隐式展开的可选项,然后立即使用它。当您尝试设置localPlayer.authenticateHandler
.
Instead you should instantiate GKLocalPlayer like so:
相反,您应该像这样实例化 GKLocalPlayer :
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:
请注意,目前 Game Center 和 Swift 存在问题。你的代码会工作,但localPlayer.authenticated
永远不会被设置为 true。此问题在此处跟踪:
http://www.openradar.me/17825348
http://www.openradar.me/17825348
Credit to: http://www.stuarticus.net/blog/2014/7/game-center-authentication-and-swiftfor filing the radar ticket.
归功于:http: //www.stuarticus.net/blog/2014/7/game-center-authentication-and-swift提交雷达票。
回答by YannSteph
You can use that, I create a simple class for iOS game center in github Easy Class Game Center Swifthttps://github.com/DaRkD0G/Easy-Game-Center-Swift
你可以使用它,我在 github Easy Class Game Center Swift https://github.com/DaRkD0G/Easy-Game-Center-Swift 中为 iOS 游戏中心创建了一个简单的类