2015-10-20 94 views
1

我已经设法让我的游戏在启动时将玩家签名到游戏中心,但是当达到高分时,它会将其保存在游戏中而不是发送到游戏中心。只要我把游戏将不会向游戏中心发送高分

[self reportScore]; 

它似乎使模拟器崩溃,我重视我的看法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 
+0

您可以尝试手动设置leaderboardIdentifier而无需查询GKLocalPlayer并查看是否修复了此问题? –

+0

没有运气,我是一个初学xcode,所以会很高兴指向正确的方向,我可以学习如何在我的应用程序中实现游戏中心。我只需要已经保存的游戏中的高分被发送到我在itunes中创建的游戏中心排行榜连接 – uz7

回答

0

你可以试试这个简单的片断是否适合您按照本教程? _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"); 
    } 
} 

此代码来自我的测试原型,我用它来解决游戏邀请和评分提交工作。代码提交的代码没有其他部分。

+0

我收到一条错误消息,说这行中使用未声明的标识符'_localPlayer'GKLocalPlayer * lp = _localPlayer; – uz7

+0

如上所述,在我的情况下,_localPlayer是一个实例变量。我改变了它,以便使用[GKLocalPlayer localPlayer]来拉取实例。 –

+0

你会如何建议我将它合并到我的代码中? – uz7