quick-cocos2d-x GameCenter 排行榜

1、编写自己的oc类 GameKitHelper.h和GameKitHelper.m

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface GameKitHelper : NSObject <GKLeaderboardViewControllerDelegate, GKAchievementViewControllerDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate>{
    BOOL gameCenterAvailable;
    BOOL userAuthenticated;
}
@property (assign, readonly) BOOL gameCenterAvailable;
+ (GameKitHelper *)sharedGameKitHelper;
- (void) authenticateLocalUser;

- (void) showLeaderboard;
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController;
- (void) reportScore: (int64_t) score forCategory: (NSString*) category;
@end
//
//  GameKitHelper.m
//  nocmd
//
//  Created by House on 14-5-7.
//
//

#import "GameKitHelper.h"

@implementation GameKitHelper

@synthesize gameCenterAvailable;
static GameKitHelper *sharedHelper = nil;
static UIViewController* currentModalViewController = nil;
+ (GameKitHelper *) sharedGameKitHelper {
    if (!sharedHelper) {
        sharedHelper = [[GameKitHelper alloc] init];
    }
    return sharedHelper;
}
//用于验证
- (BOOL)isGameCenterAvailable {
    // check for presence of GKLocalPlayer API
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
    
    // check if the device is running iOS 4.1 or later
    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;
    }
    
}
- (void)authenticateLocalUser {
    
    if (!gameCenterAvailable) return;
    
    NSLog(@"Authenticating local user...");
    if ([GKLocalPlayer localPlayer].authenticated == NO) {
        [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
    } else {
        NSLog(@"Already authenticated!");
    }
}
//显示排行榜
- (void) showLeaderboard
{
    if (!gameCenterAvailable) return;
    
    GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
    if (leaderboardController != nil) {
        leaderboardController.leaderboardDelegate = self;
        
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        currentModalViewController = [[UIViewController alloc] init];
        [window addSubview:currentModalViewController.view];
        [currentModalViewController presentModalViewController:leaderboardController animated:YES];
    }
    
}
//关闭排行榜回调
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
    if(currentModalViewController !=nil){
        [currentModalViewController dismissModalViewControllerAnimated:NO];
        [currentModalViewController release];
        [currentModalViewController.view removeFromSuperview];
        currentModalViewController = nil;
    }
}
// 上传一个分数
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
    scoreReporter.value = score;
    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
        if (error != nil)
        {
            // handle the reporting error
            NSLog(@"上传分数出错.");
            //If your application receives a network error, you should not discard the score.
            //Instead, store the score object and attempt to report the player’s process at
            //a later time.
        }else {
            NSLog(@"上传分数成功");
        }
        
    }];
}
@end


2、AppController.mm中  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法里面添加 [[GameKitHelper sharedGameKitHelper] authenticateLocalUser];认证GameCenter 

3、显示排行榜数据。在AppController.mm中  添加showLeaderBoard方法,在lua中用luaoc调用

-- 显示排行榜
function showLeaderBoard(  )
    if device.platform == "ios" then
        local args = {
            platform = platform
        }
        local ok, ret = luaoc.callStaticMethod("AppController", "showLeaderBoard", nil) --没有返回默认nil
        if ok then
            print("getIsValid,ret:",ret)
            return ret
        else
            print("shareEvent error code = ", ret)
            return nil
        end
    end
end


// 显示排行
+ (void) showLeaderBoard{
    [[GameKitHelper sharedGameKitHelper] showLeaderboard];
}

4、提交分数。AppController.mm中  添加updateScore方法,在lua中调用方法

-- 上传分数
function updateScore( score )
    if device.platform == "ios" then
        local args = {
            platform = platform,
            score = score
        }
        local ok, ret = luaoc.callStaticMethod("AppController", "updateScore", args) --没有返回默认nil
        if ok then
            print("getIsValid,ret:",ret)
            return ret
        else
            print("shareEvent error code = ", ret)
            return nil
        end
    end
end


// 上传分数
+(void) updateScore:(NSDictionary *) dict
{
    NSInteger score = [[dict objectForKey:@"score"] intValue];
    [[GameKitHelper sharedGameKitHelper] reportScore:score forCategory:@"排行榜名称"];
}




你可能感兴趣的:(lua,quick-cocos2d-x,GameCenter排行)