游戏不会将高分发送到游戏中心
Game won't send high score to Game Center
我已经设法让我的游戏在启动时将玩家登录到游戏中心,但是当达到高分时,它只是将其保存在游戏中而不是将其发送到游戏中心。无论我把
[self reportScore];
它似乎让模拟器崩溃了,我附上了我的视图 controller.m 文件,如果你能帮我一个方法让我的游戏将高分发送到游戏中心,这样我就可以继续发布leaderboard 在应用程序中显示排行榜。我正在按照 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
你能试试这个简单的片段是否适合你吗? _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");
}
}
这段代码来自我的一个测试原型,我用它来解析游戏邀请并且分数提交刚刚成功。代码中没有其他部分处理分数提交。
我已经设法让我的游戏在启动时将玩家登录到游戏中心,但是当达到高分时,它只是将其保存在游戏中而不是将其发送到游戏中心。无论我把
[self reportScore];
它似乎让模拟器崩溃了,我附上了我的视图 controller.m 文件,如果你能帮我一个方法让我的游戏将高分发送到游戏中心,这样我就可以继续发布leaderboard 在应用程序中显示排行榜。我正在按照 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
你能试试这个简单的片段是否适合你吗? _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");
}
}
这段代码来自我的一个测试原型,我用它来解析游戏邀请并且分数提交刚刚成功。代码中没有其他部分处理分数提交。