简体   繁体   中英

Game Center Achievement Looping

I'm having a bit of trouble trying to figure out the best way to report achievement progress to Game Center. The issue that I run into is that because I'm checking my score during the update cycle achievements are being reported continuously to game center. This triggers the banner to loop continuously until the game is forcibly quit.

I assume I need to trip some sort of bool to stop reporting when the achievement has been unlocked, but I'm just having trouble figuring out how to go about writing this trigger.

below is the checkAchievements() function which is called inside of update(). This is the latest of many attempts at writing something along these lines - every time I end up with the same issue.

 func checkAchievements() {
       
        var identifier : String? = nil
        var percentComplete : Double = 0
        
        switch GameScene.score {
        
        case 1...5 :    identifier = "5points"
                        percentComplete = 100.0
        case 6...10 :   identifier = "10points"
                        percentComplete = 100.0
        default : identifier = nil
        
        }
        
        if identifier != nil {
            let achievement = GKAchievement(identifier: identifier!)
            achievement.showsCompletionBanner = true
            achievement.percentComplete = percentComplete
        
            GKAchievement.report([achievement], withCompletionHandler: nil)
        }
        
       
    }

Figured it out. Here's the solution I came up with, Probably can be improved. but at least we're no longer stuck looping achievements as they happen.

    func checkAchievements() {
       
        var achievements : [GKAchievement] = []
        
        if GameScene.score > 1 && gettingAquainted.percentComplete < 100 {
            gettingAquainted.percentComplete = 100
            achievements.append(gettingAquainted)
        }
        
        if GameScene.score > 5 && movingUp.percentComplete < 100 {
            movingUp.percentComplete = 100
            achievements.append(movingUp)
        }
        
        if GameScene.score > 10 && nowYourCooking.percentComplete < 100 {
            nowYourCooking.percentComplete = 100
            achievements.append(nowYourCooking)
        }
        
        if GameScene.score > 20 && lookAtYou.percentComplete < 100 {
            lookAtYou.percentComplete = 100
            achievements.append(lookAtYou)
        }
        
        if achievements.count > 0{
            GKAchievement.report(achievements, withCompletionHandler: nil)
            achievements.removeAll()
        }
       
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM