【问题标题】:Game won't send high score to Game Center游戏不会向游戏中心发送高分
【发布时间】:2015-10-22 20:53:35
【问题描述】:

我已经设法让我的游戏在发布时让玩家登录游戏中心,但是当达到高分时,它只是将其保存在游戏中,而不是将其发送到游戏中心。无论我把

[self reportScore];

它似乎使模拟器崩溃,如果你能帮助我提供一种方法让我的游戏将高分发送到游戏中心,我已经附上了我的 view controller.m 文件,这样我就可以继续将排行榜发布到显示上应用程序中的排行榜。顺便说一下http://www.appcoda.com/ios-game-kit-framework/

 #import "ViewController.h"
 #import <GameKit/GameKit.h>
 #import <UIKit/UIKit.h>
 #import <iAd/iAd.h>

 @interface ViewController ()

 @property (nonatomic, strong) NSString *leaderboardIdentifier;
 @property (nonatomic,assign) BOOL gameCenterEnabled;

 -(void)authenticateLocalPlayer;
 -(void)reportScore;

 @end

 @implementation ViewController

-(void)reportScore{
     GKScore *score = [[GKScore alloc]   initWithLeaderboardIdentifier:_leaderboardIdentifier];
     score.value = HighScoreNumber;

     [GKScore reportScores:@[score] withCompletionHandler:^(NSError  *error) {
         if (error != nil) {
             NSLog(@"%@", [error localizedDescription]);
         }
     }];
}

-(void)authenticateLocalPlayer;{
     GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

     localPlayer.authenticateHandler = ^(UIViewController  *viewController, NSError *error){
         if (viewController != nil) {
             [self presentViewController:viewController animated:YES  completion:nil];
         } else {
             if ([GKLocalPlayer localPlayer].authenticated) {
                 _gameCenterEnabled = YES;

                // Get the default leaderboard identifier.
                [[GKLocalPlayer localPlayer]  loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString         *leaderboardIdentifier, NSError *error) {
                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    } else {
                        _leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            } else {
                _gameCenterEnabled = NO;
            }
        }
    };
}

- (void)viewDidLoad
{
    [self authenticateLocalPlayer];

     HighScoreNumber = [[NSUserDefaults standardUserDefaults]  integerForKey:@"HighScoreSaved"];
     HighScore.text = [NSString stringWithFormat:@"High Score: %li", (long)HighScoreNumber];

     [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark iAd Delegate Methods

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:1];

    [banner setAlpha:1];

    [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:1];

    [banner setAlpha:0];

    [UIView commitAnimations];
}

@end

【问题讨论】:

  • 您可以尝试手动设置 leaderboardIdentifier 而不查询 GKLocalPlayer,看看这是否解决了它?
  • 不走运,我是 xcode 的初学者,所以希望能指出正确的方向,在那里我可以学习如何在我的应用程序中实现游戏中心。我只需要将游戏保存的高分发送到我在 iTunes Connect 中创建的游戏中心排行榜

标签: ios objective-c game-center game-center-leaderboard


【解决方案1】:

你能试试这个简单的 sn-p 是否适合你吗? _localPlayer 指的是使用身份验证处理程序设置的实例变量。

- (IBAction)doAddAScore:(id)sender {
    GKLocalPlayer *lp = [GKLocalPlayer localPlayer];
    NSInteger score = 100;
    if (lp && lp.isAuthenticated) {
        NSString *lbid = @"your.leaderboard.id";
        GKScore *gkScore = [[GKScore alloc] initWithLeaderboardIdentifier:lbid player:lp];
        gkScore.value = score;
        [GKScore reportScores:@[gkScore] withCompletionHandler:^(NSError * _Nullable error) {
            NSLog(@"GKScore::reportScores completed - error : %@", error);
        }];
    } else {
        NSLog(@"reporting score: localPlayer nil or not authenticated");
    }
}

这段代码来自我的一个测试原型,我用它来解决游戏邀请,并且分数提交刚刚奏效。代码中没有其他部分处理分数提交。

【讨论】:

  • 我收到一条错误消息,说在这一行 GKLocalPlayer *lp = _localPlayer; 上使用未声明的标识符“_localPlayer”
  • 如前所述,在我的例子中,_localPlayer 是一个实例变量。我对其进行了更改,以便它使用 [GKLocalPlayer localPlayer] 拉取实例。
  • 你建议我如何将它合并到我的代码中?
  • 您可以在authenticateLocalPlayer中的_gameCenterEnabled = YES;下方添加[self doAddAScore:nil];
  • 啊! -(void)authenticateLocalPlayer 后面有一个分号。你的代码中也有吗?如果是,请删除它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多