我已经成功地添加了游戏中心的功能到我的应用程序。当应用程序打开时,它成功地验证了用户的身份,并显示了“欢迎回来(UserName)”的横幅。
然而,我不知道如何在游戏中增加一个领头羊。我想知道是否有人能帮我。A:帮助我理解如何将我在iTunes中制作的主板连接到应用程序中,并让它的价值得到高分。和B:让领导板显示在应用程序与所有的排名。
到目前为止,我应用程序中gamecenter的所有代码都在下面。
接口文件:
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
@interface GCHelper : NSObject {
BOOL gameCenterAvailable;
BOOL userAuthenticated;
}
@property (assign, readonly) BOOL gameCenterAvailable;
+ (GCHelper *)sharedInstance;
-(void)authenticateLocalUser;
@end实施文件:
#import "GCHelper.h"
@implementation GCHelper
@synthesize gameCenterAvailable;
#pragma mark initialization
static GCHelper *sharedHelper = nil;
+ (GCHelper *) sharedInstance {
if (!sharedHelper) {
sharedHelper = [[GCHelper alloc] init];
}
return sharedHelper;
}
- (BOOL)isGameCenterAvailable {
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
NSString *reqSysVer = @"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer
options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
- (id) init {
if ((self = [super init])) {
gameCenterAvailable = [self isGameCenterAvailable];
if(gameCenterAvailable) {
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
}
return self;
}
-(void)authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
NSLog(@"Authentication changed: player authenticated.");
userAuthenticated = TRUE;
} else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
NSLog(@"Authentication changed: player not authenticated");
userAuthenticated = FALSE;
}
}
#pragma mark User Functions
-(void) authenticateLocalUser {
if(!gameCenterAvailable) return;
NSLog(@"Authentication local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
} else {
NSLog(@"Already authenticated!");
}
}
@end好吧,那么。该代码现在正在工作,但当访问领导板的代码返回一个错误时,我没有处理它的代码,而只是使应用程序陷入冻结状态,使其无法工作。
调用代码以访问主板:
- (void) presentLeaderboards
{
GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
gameCenterController.gameCenterDelegate = self;
[self presentViewController:gameCenterController animated:YES completion:nil];
}发布于 2014-07-27 05:11:49
要设置一个领导板,请访问iTunes连接>管理您的应用程序>您的应用程序>管理游戏中心>添加领导板>单一领导板。
给你的领导板一个名称和所有所需的组织信息。
将此方法添加到GCHelper.m中
-(void) submitScore:(int64_t)score Leaderboard: (NSString*)leaderboard
{
//1: Check if Game Center
// features are enabled
if (!_gameCenterFeaturesEnabled) {
return;
}
//2: Create a GKScore object
GKScore* gkScore =
[[GKScore alloc]
initWithLeaderboardIdentifier:leaderboard];
//3: Set the score value
gkScore.value = score;
//4: Send the score to Game Center
[gkScore reportScoreWithCompletionHandler:
^(NSError* error) {
[self setLastError:error];
BOOL success = (error == nil);
if ([_delegate
respondsToSelector:
@selector(onScoresSubmitted:)]) {
[_delegate onScoresSubmitted:success];
}
}];
}并将其添加到您的GCHelper.h中
-(void) submitScore:(int64_t)score Leaderboard: (NSString*)leaderboard;现在,在游戏的.m中,将此方法添加到游戏结束时调用的任何方法中:
[[GCHelper sharedGameKitHelper] submitScore:highScore Leaderboard:LeaderboardName];在本例中,highScore是分数的int_64值,LeaderboardName是一个NSString,等于在iTunes连接中设置的领导板标识符。还一定要添加游戏中心的功能到您的应用程序。
在那之后,你应该能够提交高分!
还将此添加到GCHelper.m中。
-(void) setLastError:(NSError*)error {
_lastError = [error copy];
if (_lastError) {
NSLog(@"GameKitHelper ERROR: %@", [[_lastError userInfo]
description]);
}
}并将其添加到GCHelper.h中。
@property (nonatomic, assign)id<GCHelperProtocol> delegate;这是我的GCHelper.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// Include the GameKit framework
#import <GameKit/GameKit.h>
// Protocol to notify external
// objects when Game Center events occur or
// when Game Center async tasks are completed
@protocol GCHelperProtocol<NSObject>
-(void) onScoresSubmitted:(bool)success;
@end
@interface GCHelper : NSObject
@property (nonatomic, assign)id<GCHelperProtocol> delegate;
// This property holds the last known error
// that occured while using the Game Center API's
@property (nonatomic, readonly) NSError* lastError;
+ (id) sharedGameKitHelper;
// Player authentication, info
-(void) authenticateLocalPlayer;
//Scores
-(void) submitScore:(int64_t)score Leaderboard: (NSString*)leaderboard;
@end在这里我的GCHelper.m:
#import "GCHelper.h"
@interface GCHelper ()
<GKGameCenterControllerDelegate> {
BOOL _gameCenterFeaturesEnabled;
}
@end
@implementation GCHelper
#pragma mark Singleton stuff
+(id) sharedGameKitHelper {
static GCHelper *sharedGameKitHelper;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedGameKitHelper =
[[GCHelper alloc] init];
});
return sharedGameKitHelper;
}
#pragma mark Player Authentication
-(void) authenticateLocalPlayer {
GKLocalPlayer* localPlayer =
[GKLocalPlayer localPlayer];
localPlayer.authenticateHandler =
^(UIViewController *viewController,
NSError *error) {
[self setLastError:error];
if (localPlayer.authenticated) {
_gameCenterFeaturesEnabled = YES;
} else if(viewController) {
[self presentViewController:viewController];
} else {
_gameCenterFeaturesEnabled = NO;
}
};
}
#pragma mark Property setters
-(void) setLastError:(NSError*)error {
_lastError = [error copy];
if (_lastError) {
NSLog(@"GameKitHelper ERROR: %@", [[_lastError userInfo]
description]);
}
}
#pragma mark UIViewController stuff
-(UIViewController*) getRootViewController {
return [UIApplication
sharedApplication].keyWindow.rootViewController;
}
-(void)presentViewController:(UIViewController*)vc {
UIViewController* rootVC = [self getRootViewController];
[rootVC presentViewController:vc animated:YES
completion:nil];
}
#pragma mark Scores
- (void) reportAchievementWithID:(NSString*) AchievementID {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if(error) NSLog(@"error reporting ach");
for (GKAchievement *ach in achievements) {
if([ach.identifier isEqualToString:AchievementID]) { //already submitted
return ;
}
}
GKAchievement *achievementToSend = [[GKAchievement alloc] initWithIdentifier:AchievementID];
achievementToSend.percentComplete = 100;
achievementToSend.showsCompletionBanner = YES;
[achievementToSend reportAchievementWithCompletionHandler:NULL];
}];
}
-(void) submitScore:(int64_t)score Leaderboard: (NSString*)leaderboard
{
//1: Check if Game Center
// features are enabled
if (!_gameCenterFeaturesEnabled) {
return;
}
//2: Create a GKScore object
GKScore* gkScore =
[[GKScore alloc]
initWithLeaderboardIdentifier:leaderboard];
//3: Set the score value
gkScore.value = score;
//4: Send the score to Game Center
[gkScore reportScoreWithCompletionHandler:
^(NSError* error) {
[self setLastError:error];
BOOL success = (error == nil);
if ([_delegate
respondsToSelector:
@selector(onScoresSubmitted:)]) {
[_delegate onScoresSubmitted:success];
}
}];
}
-(void) gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
//nothing
}
@endhttps://stackoverflow.com/questions/24967767
复制相似问题