编写苹果游戏中心应用程序(翻译 1.14 向游戏中心提交成就)
来源:互联网 发布:33选6所有组合数据库 编辑:程序博客网 时间:2024/05/17 01:14
1.14 向游戏中心提交成就
问题
你已准备好在游戏中使用成就。
解决方案
使用GKAchievement类。
讨论
提交成就的过程和提交成绩(条目1.10)相似,步骤是:
1. 验证本地玩家(条目1.5)。
2. 分配和初始化GKAchievement类型的对象。分配对象有两种方法:一是使用initWithIdentifier:方法;二是先简单地使用init方法,然后再使用setIdentifier:方法设置标识符。标识符是我们创建成就(条目1.13)时指定的“Achievement ID”。
3. 使用对象的setPercentComplete:方法设置成就的完成度(0%-100%)。
4. 调用reportAchievementWithCompletionHandler:方法,传递一个返回void、接受一个NSError类型参数的块对象。
下面的示例代码提交ID为“MGL1HP1C”(条目1.13)的成就50%的完成度
- (BOOL) reportAchievementWithID:(NSString *)paramAchievementID
percentageCompleted:(double)paramPercentageCompleted{
BOOL result = NO;
if ([paramAchievementID length] == 0){
NSLog(@"Achievement ID cannot be empty.");
return NO;
}
GKAchievement *achievement =
[[[GKAchievement alloc] initWithIdentifier:paramAchievementID]
autorelease];
NSLog(@"Setting percentage to %.02f", paramPercentageCompleted);
[achievement setPercentComplete:paramPercentageCompleted];
NSLog(@"Reporting the achievement...");
[achievement reportAchievementWithCompletionHandler:^(NSError *error) {
if (error == nil){
NSLog(@"Successfully reported the achievement.");
} else {
NSLog(@"Failed to report the achievement. %@", error);
}
}];
return result;
}
- (void) authenticateLocalPlayerAndReportAchievement{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
NSLog(@"Authenticating the local player...");
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil){
NSLog(@"Successfully authenticated the local player.");
NSLog(@"Reporting achievement...");
[self reportAchievementWithID:@"MGL1HP1C"
percentageCompleted:50.0f];
} else {
NSLog(@"Failed to authenticate the local player. %@", error);
}
}];
}
在调用authenticateLocalPlayerAndReportAchievement方法后,控制台输出类似如下的结果(除非有错误发生;在这种情况下,将输出错误信息):
Authenticating the local player...
Successfully authenticated the local player.
Reporting achievement...
Setting percentage to 50.00
Reporting the achievement...
Successfully reported the achievement.
在成就提交到游戏中心之后,如果成就没有被设置为隐藏,那么本地玩家就可以打开游戏中心应用程序查看它(当然,还有该玩家使用该程序所获得的所有其他成就),如图1-14所示。
图 1-14 iOS模拟器上的成就
一旦玩家选择“Achievements”选项,他将看到程序提交给游戏中心的所有成就,以及每个成就的进度,如图1-15所示。
图 1-15 iOS模拟器上的成就进度
在iTunes Connect中创建成就之后,可能需要延迟一小段时间,你才能提交该成就的进度。如果你得到的错误说成就不存在,但你确信它存在,请等待5-10分钟,然后重试。
- 编写苹果游戏中心应用程序(翻译 1.14 向游戏中心提交成就)
- 编写苹果游戏中心应用程序(翻译 1.16 向玩家显示成就)
- 编写苹果游戏中心应用程序(翻译 1.10 向排行榜提交得分)
- 编写苹果游戏中心应用程序(翻译 1.13 在iTunes Connect中创建成就)
- 编写苹果游戏中心应用程序(翻译 1.15 编程获取成就信息)
- 编写苹果游戏中心应用程序(翻译 1.3 为iOS应用程序设置游戏中心)
- 编写苹果游戏中心应用程序(翻译 1.12 向玩家显示排行榜)
- 编写苹果游戏中心应用程序(翻译 1.2 创建游戏中心账户)
- 编写苹果游戏中心应用程序(翻译 1.5 在游戏中心验证本地玩家)
- 编写苹果游戏中心应用程序(翻译 1.7 在游戏中心添加朋友)
- 编写苹果游戏中心应用程序(翻译 1.4 添加游戏工具包框架)
- 编写苹果游戏中心应用程序(翻译 1.17 多人游戏支持和竞技)
- 编写苹果游戏中心应用程序(翻译 1.1 GCD和块对象介绍)
- 编写苹果游戏中心应用程序(翻译 1.6 获取本地玩家的信息)
- 编写苹果游戏中心应用程序(翻译 1.8 获取本地玩家的好友信息)
- 编写苹果游戏中心应用程序(翻译 1.9 在iTunes Connect中创建排行榜)
- 编写苹果游戏中心应用程序(翻译 1.11 编程获取排行榜信息)
- 编写苹果游戏中心应用程序(翻译 1.18 在多人游戏中处理玩家的状态变更)
- 重定向和请求分派的比较
- 串口通信中的int float型数据的处理和发送
- Mutex/Semphore/Binary Semphore的异同
- 如何学习Android及如何利用android来赚钱
- VC使用技巧
- 编写苹果游戏中心应用程序(翻译 1.14 向游戏中心提交成就)
- 深入浅出VA函数
- java文件操作--Thinking in java
- ZK 客户端事件-状态对应关系图
- myeclipse 中使用junit 调试jpa报错 java.lang.NoClassDefFoundError: org/slf4j/helpers/NOPLoggerFactory
- apache Rewrite拟静态
- .NET DataGridView列排序
- IS环境下配置PHP5+MySql+PHPMyAdmin
- archlinux安装