编写苹果游戏中心应用程序(翻译 1.10 向排行榜提交得分)

 

1.10 向排行榜提交得分

问题

    你已经在iTunes Connect中创建了至少一个排行榜,现在你想将玩家的成绩保存到排行榜。

解决方案

    使用GKScore类的实例方法reportScoreWithCompletionHandler:

讨论

    如果已经创建了排行榜,则遵循以下的步骤,以向排行榜提交成绩:

    1. 验证本地玩家(条款1.5)。

    2. 创建GKScore的实例,设置该实例的成绩分类为要使用的“Leaderboard ID”。

    3. 设置实例的value属性。

    4. 使用GKScore类的实例方法reportScoreWithCompletionHandler:。该方法接收一个参数,它必须是一个返回void、接受一个NSError类型的参数的块对象。你可以使用NSError参数来判断处理过程中是否有错误发生:

    - (BOOL) reportScore:(NSUInteger)paramScore
    toLeaderboard:(NSString *)paramLeaderboard{

        __block BOOL result = NO;

        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

        if ([localPlayer isAuthenticated] == NO){
            NSLog(@"You must authenticate the local player first.");
            return NO;
        }

        if ([paramLeaderboard length] == 0){
            NSLog(@"Leaderboard identifier is empty.");
            return NO;
        }

        GKScore *score = [[[GKScore alloc]
                           initWithCategory:paramLeaderboard] autorelease];

        score.value = (int64_t)paramScore;

        NSLog(@"Attempting to report the score...");

        [score reportScoreWithCompletionHandler:^(NSError *error) {
            if (error == nil){
                NSLog(@"Succeeded in reporting the error.");
                result = YES;
            } else {
                NSLog(@"Failed to report the error. Error = %@", error);
            }
        }];

        return result;

    }

    - (void) authenticateLocalPlayerAndReportScore{

        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

        if ([localPlayer isAuthenticated] == YES){
            NSLog(@"The local player has already authenticated.");
            return;
        }

        [localPlayer authenticateWithCompletionHandler:^(NSError *error) {

            if (error == nil){
                NSLog(@"Successfully authenticated the local player.");

                [self reportScore:10
                    toLeaderboard:@"MGL1LB"];
            } else {
                NSLog(@"Failed to authenticate the player with error = %@", error);
            }
        }];
    }

    authenticateLocalPlayerAndReportScore方法将验证本地玩家,向“Reference ID”为“MGL1LB”(条目1.9)的排行榜提交成绩(10分)。我的控制台窗口输出结果如下:

    Successfully authenticated the local player.
    Attempting to report the score...
    Succeeded in reporting the error.

    如果向不能存在的排行榜提交数据,则从reportScoreWithCompletionHandler:方法收到的错误将类似如下:

    Error Domain=GKErrorDomain Code=17 "The requested operations could
    not be completed because one or more parameters are invalid."
    UserInfo=0x5f43a90 {NSUnderlyingError=0x5f09390 "The operation
    couldn't be completed. status = 5053", NSLocalizedDescription=The
    requested operations could not be completed because
    one or more parameters are invalid.}

    查看提交到游戏中心(沙盒服务器)的成绩有三种方法:

    1. 在iOS模拟器中使用游戏中心应用程序。

    2. 用程序获取成绩(条目1.11)。

    3. 在程序的用户界面中显示排行榜(条目1.12)。

    后两种显示排行榜成绩给玩家的方法将在各自对应的章节介绍。此处,我只讲解如何使用iOS模拟器显示成绩:

    1. 在模拟器中打开游戏中心应用程序。

    2. 登录为本地玩家。

    3. 在屏幕下方导航到“Games”页签。

    4. 选择提交了成绩的游戏,你将看到游戏的菜单,如图1-10。


图 1-10 iOS模拟器中的游戏菜单

    5. 选择“Leaderboard”,将看到排行榜中的成绩(如图1-11)。


图 1-11 iOS模拟器中排行榜界面

    图1-11中的成绩是10,因为我们先前提交的成绩就是10。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值