2014-07-26 142 views
1

我已經成功地將Game Center功能添加到我的應用程序。當應用程序打開時,它成功驗證用戶並顯示「歡迎回來(用戶名)」橫幅。iOS 7排行榜遊戲中心

但是,我不確定如何將排行榜添加到遊戲中。我想知道是否有人可以幫助我答:幫助我瞭解如何將我在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]; 

} 
+0

您是否在Game Center上使用了Ray Wenderlich教程?你的代碼看起來很熟悉 – WMios

+0

這是一個YouTube教程,結束之前,它告訴我如何添加排行榜我不知道上傳者 – user3752308

+0

看看我的答案,這是非常直接的和有效的。我在應用商店中有兩款遊戲,並提交了第三款遊戲,他們都使用這個遊戲。這也適用於單個遊戲中的多個排行榜! – WMios

回答

1

要建立一個領先進入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值,LeaderboardNameNSString等於您在iTunes Connect中設置的排行榜標識符。另外請確保爲您的應用程序添加Game Center功能。

之後,你應該能夠提交高分!

同時添加這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 
} 

@end 
+0

這是一個鏈接到我的項目和我使用此代碼的錯誤的屏幕截圖。 http://aphprojects.files.wordpress.com/2014/07/screen-shot-2014-07-28-at-11-35-44-am.png – user3752308

+0

增加了您發佈的額外代碼併產生了這些新錯誤。 https://fbcdn-sphotos-ha.akamaihd.net/hphotos-ak-xpf1/v/t35.0-12/10587088_748152948574512_1025435203_o.jpg?oh=7029983e53c9a3e875b79853585e4941&oe=53D7BE78&__gda__=1406647272_2ccc848c1c28c4339e981d9e1b16ae29 – user3752308

+0

好吧只要看看我的回答底部,我複製並粘貼了我的GCHelper.h和GCHelper.m。這應該可以解決一切。 – WMios