作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款游戏,该游戏会在玩家达到特定分数时使用 Game Center 成就。然而,在我的测试中,我意识到每当达到一定分数时就会显示横幅,无论它们是否已经这样做了。
我想我需要使用 loadAchievementsWithCompletionHandler 来检查成就是否已经完成。但是,我有点不确定如何执行此操作。这是我的代码:
func checkAchievements() {
var identifier : String? = nil
var index : Int!
var percentComplete : Double = 0
switch(score)
{
case 30:
identifier = "30"
index = 0 // Index for array made in loadAchievementsWithCompletionHandler
percentComplete = 100.0
case 60:
identifier = "60"
index = 1
percentComplete = 100.0
case 90:
identifier = "90"
index = 2
percentComplete = 100.0
case 120:
identifier = "120"
index = 3
percentComplete = 100.0
case 150:
identifier = "150"
index = 4
percentComplete = 100.0
default:
identifier = nil
}
if identifier != nil {
let achievement = GKAchievement(identifier: identifier)
achievement.showsCompletionBanner = true
GKAchievement.loadAchievementsWithCompletionHandler({(achievement, var error) in
if (error != nil) {
// Not sure what to put here
}
})
}
}
到目前为止,我不太确定最后一部分是否正确,我也不知道如何访问数组,以便检查成就是否已完成。
最佳答案
你可以使用它,我在 github 中为 iOS 游戏中心创建了一个简单的类 https://github.com/DaRkD0G/Easy-Game-Center-Swift或者我有办法开始添加此代码
// Game Center
let gameCenterPlayer=GKLocalPlayer.localPlayer()
var canUseGameCenter:Bool = false {
didSet{if canUseGameCenter == true {// load prev. achievments form Game Center
gameCenterLoadAchievements()}
}}
var gameCenterAchievements=[String:GKAchievement]()
// MARK: Game Center
// load prev achievement granted to the player
func gameCenterLoadAchievements(){
// load all prev. achievements for GameCenter for the user to progress can be added
var allAchievements=[GKAchievement]()
GKAchievement.loadAchievementsWithCompletionHandler({ (allAchievements, error:NSError!) -> Void in
if error != nil{
println("Game Center: could not load achievements, error: \(error)")
} else {
for anAchievement in allAchievements {
if let oneAchievement = anAchievement as? GKAchievement {
self.gameCenterAchievements[oneAchievement.identifier]=oneAchievement}
}
}
})
}
// add progress to an achievement
func gameCenterAddProgressToAnAchievement(progress:Double,achievementID:String) {
if canUseGameCenter == true { // only update progress if user opt-in to use Game Center
// lookup if prev progress is logged for this achievement = achievement is already know (and loaded) form Game Center for this user
var lookupAchievement:GKAchievement? = gameCenterAchievements[achievementID]
if let achievement = lookupAchievement {
// found the achievement with the given achievementID, check if it already 100% done
if achievement.percentComplete != 100 {
// set new progress
achievement.percentComplete = progress
if progress == 100.0 {achievement.showsCompletionBanner=true} // show banner only if achievement is fully granted (progress is 100%)
// try to report the progress to the Game Center
GKAchievement.reportAchievements([achievement], withCompletionHandler: {(var error:NSError!) -> Void in
if error != nil {
println("Couldn't save achievement (\(achievementID)) progress to \(progress) %")
}
})
}
else {// achievemnt already granted, nothing to do
println("DEBUG: Achievement (\(achievementID)) already granted")}
} else { // never added progress for this achievement, create achievement now, recall to add progress
println("No achievement with ID (\(achievementID)) was found, no progress for this one was recoreded yet. Create achievement now.")
gameCenterAchievements[achievementID] = GKAchievement(identifier: achievementID)
// recursive recall this func now that the achievement exist
gameCenterAddProgressToAnAchievement(progress, achievementID: achievementID)
}
}
}
在 ViewDidLoad 之后
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.gameCenterPlayer.authenticateHandler={(var gameCenterVC:UIViewController!, var gameCenterError:NSError!) -> Void in
if gameCenterVC != nil {
//showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.
//showAuthenticationDialogWhenReasonable(gameCenterVC!)
self.presentViewController(gameCenterVC, animated: true, completion: { () -> Void in
// no idea
})
}
else if self.self.gameCenterPlayer.authenticated == true {
self.self.canUseGameCenter = true
} else {
self.canUseGameCenter = false
}
if gameCenterError != nil
{ println("Game Center error: \(gameCenterError)")}
}
}
然后在 Button 中为你的成就添加这个
@IBAction func action(sender: AnyObject) {
gameCenterLoadAchievements()
gameCenterAddProgressToAnAchievement(100.00, achievementID: "vous_etre_encore_vivant")
}
瞧瞧瞧
;-) 法国开发者谢谢:RMeeuws
关于ios - swift - loadAchievementsWithCompletionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25932718/
我正在制作一款游戏,该游戏会在玩家达到特定分数时使用 Game Center 成就。然而,在我的测试中,我意识到每当达到一定分数时就会显示横幅,无论它们是否已经这样做了。 我想我需要使用 loadAc
我在初始化时使用函数[GKAchievement loadAchievementsWithCompletionHandler:] 恢复当前玩家成就。但是从未调用过 completionHander。
我是一名优秀的程序员,十分优秀!