强大的游戏中心成就代码 [英] Robust Game Center Achievement code

查看:141
本文介绍了强大的游戏中心成就代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几乎所有向游戏中心提交成就的例子中,我都会看到这段代码

In just about every example of submitting achievements to Game Center, I see this code

[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
    if (error != nil)
    {
         // Retain the achievement object and try again later (not shown).
    }
}];

问题是,一个小评论约占99%的工作量。我花了最后几个小时试图解决这个问题,它似乎是一组无休止的发送和重发以及保存和加载数据的边缘情况。

Problem is, that one little comment is about 99% of the work. I've spent the last few hours trying to figure this out and it seems to be an endless set of edge cases of sending and resending and save and loading data.

有谁知道一个很好的教程(或示例代码),这实际上解释了困难的部分?

Does anyone know of a nice tutorial (or sample code) on this that actually explains the hard part?

这不像将它们保存到文件并稍后加载它们那么简单。当你开始不得不保留多个成就并稍后提交它们然后它们全部回来失败(在块中!)并且你必须再次保存它们时,你会遇到麻烦...快速/安全...因为应用程序可能会退出并且你不想放松它们。

It's not as simple as just saving them to a file and loading them later. You get into trouble when you start having to retain multiple achievements and submit them later and then they all come back failed (in blocks!) and you have to save them again... quickly/safely... because the App might quit and you don't want to loose them.

我把头发拉出来。

推荐答案

我觉得你不需要拔头发。

I don't think you need to pull your hair out.

我认为基本型号是这样的:

I think the basic model is this:

(a)独立于游戏中心,您的游戏有一个保存状态(无论如何,您总是需要将玩家恢复到游戏退出/后退时的状态)。此状态应包括恢复游戏状态所需的所有正常内容,此外还应包含游戏中所有成就的标记。

(a) Independent of Game Center, your game has a saved state (which you always need anyway, to restore the player to where he was when your game is quit/backgrounded). This state should include all the normal stuff you need to restore your game state, plus it should include flags for all of the achievements in your game.

(b)游戏中心还存储游戏中的所有成就。当您连接到Game Center时,您可以下载已登录播放器的成就。 (对于谁登录的玩家来说是复杂的,但是你的问题与此无关,所以让我们假设一个真正的玩家总是登录的游戏中心玩家。)

(b) Game Center also stores all of the achievements in your game. When you connect to Game Center, you download the signed-in player's achievements. (Complications arise as to who the signed-in player is, but your question is not about that, so let's assume the "one true player" is always the signed-in Game Center player.)

(c)每当玩家取得成就时,首先在游戏保存的状态数据中更新相应的成就标志。其次,尝试告诉Game Center这个成就。如果有效,请使用新数据更新您的游戏中心成就副本。

(c) Whenever the player makes an achievement, first update the appropriate achievement flag in your game's saved state data. Second, try to tell Game Center about that achievement. If it works, update your copy of Game Centers achievements with the new data.

(d)如果它不起作用,您的持久状态会标记成就在您的Game Center状态副本中未标记。在各种方便的时间(例如,每当获得另一个成就,当您结束一个级别,当您的应用程序启动时,当您的应用程序退出时等)时,检查您自己的状态与其中的状态之间是否存在任何差异。游戏中心状态。差异是您需要重新发送到Game Center的更新。尝试发送,但如果它再次失败,只需等到下一次机会(例如,每当获得另一个成就,当你结束一个级别,当你的应用程序启动时,当你的应用程序退出时等)再试一次。您永远不会丢失数据,因为它位于用户状态的本地真实图片中。 (失败的唯一方法是,如果用户在您成功将其发送到Game Center之前卸载了您的应用,那么,您可以做什么?)

(d) If it doesn't work, you have an achievement marked in your persistent state that is not marked in your copy of the Game Center state. At various convenient times (e.g., whenever another achievement is earned, when you end a level, when your app starts, when you app quits, etc.) check if there are any discrepancies between what is in your own state vs. what is in the Game Center state. A discrepancy is an update that you need to re-send to Game Center. Try to send, but if it fails again, just wait until the next opportunity (e.g., whenever another achievement is earned, when you end a level, when your app starts, when you app quits, etc.) to try again. You will never lose the data because it's in your local, true picture of the user's state. (The only way you'd lose it is if the user uninstalls your app before you've been able to successfully get it to Game Center, but then, what can you do?)

这篇关于强大的游戏中心成就代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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