2015-10-20 89 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