cocos2d-x 添加 Game Center 游戏中心的方法

  • 本帖最后由 wangjingjing 于 2012-6-20 17:51 编辑 *

写这篇文章的主要用于给自己加强印象,帮助他人就是帮助自己.本教程基于cocos2d-x 13版本编写 主要内容是告诉大家在c++中如何添加 game center ,找了很多网络上面的代码基本上都是缺胳膊少腿的,代码不完整,对于初学者来说是痛苦的。本代码仅提供如何登陆GameCenter 显示和关闭Leaderboard(排行榜)。其他实现请参考子龙山人的博客,顺便贴下地址http://www.cnblogs.com/zilongshanren/archive/2011/06/24/2088383.html 他讲解了很多东西。如何激活GameCenter等值得学习的文章。写的很细致哦废话不说上贴上代码咯我这里主要如何使用UIViewController 来实现GameCenter的呈现

//
//  GameKitHelper.h
//  toDefendTheEarth
//
//  Created by jingjing on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.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;

@end

这是实现

//
//  GameKitHelper.m
//  toDefendTheEarth
//
//  Created by jingjing on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//


#import "GameKitHelper.h"

@implementation GameKitHelper
@synthesize gameCenterAvailable;

//静态初始化 对外接口
static GameKitHelper *sharedHelper = nil;
static UIViewController* currentModalViewController = nil;
+ (GameKitHelper *) sharedGameKitHelper {
    if (!sharedHelper) {
        sharedHelper =  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 =  systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer 
                                           options:NSNumericSearch] != NSOrderedAscending);
    
    return (gcClass && osVersionSupported);
}

- (id)init {
    if ((self = )) {
        gameCenterAvailable = ;
        if (gameCenterAvailable) {
            NSNotificationCenter *nc = 
            ;
            [nc addObserver:self 
                   selector:@selector(authenticationChanged) 
                       name:GKPlayerAuthenticationDidChangeNotificationName 
                     object:nil];
        }
    }
    return self;
}

//后台回调登陆验证
- (void)authenticationChanged { 
    
    if (.isAuthenticated &&!userAuthenticated) {
        NSLog(@"Authentication changed: player authenticated.");
        userAuthenticated = TRUE; 
    } else if (!.isAuthenticated && userAuthenticated) {
        NSLog(@"Authentication changed: player not authenticated");
        userAuthenticated = FALSE;
    }
    
}

- (void)authenticateLocalUser { 
    
    if (!gameCenterAvailable) return;
    
    NSLog(@"Authenticating local user...");
    if (.authenticated == NO) { 
         authenticateWithCompletionHandler:nil]; 
    } else {
        NSLog(@"Already authenticated!");
    }
}

//显示排行榜
- (void) showLeaderboard
{
    if (!gameCenterAvailable) return;
    
    GKLeaderboardViewController *leaderboardController =  init];
    if (leaderboardController != nil) {
        leaderboardController.leaderboardDelegate = self;
        
        UIWindow *window =  keyWindow];
        currentModalViewController =  init];
        ;
        ;
    }
    
}

//关闭排行榜回调
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
    if(currentModalViewController !=nil){
        ;
        ;
        ;
        currentModalViewController = nil;
    }
}

好了。这个来说下如何调用它们吧。先把AppDelegate.cpp 后缀修改为mm在AppDelegate.mm里引入

#import "GameKitHelper.h"

在 applicationDidFinishLaunching方法里调用,代码写在运行场景之前即可

//GameCenter登陆
 authenticateLocalUser];

到此登陆就ok啦。后面来教大家如何显示排行榜界面

//显示排行榜
 showLeaderboard];

只要把此代码写在你想要调用的方法中就可以了。别忘记了在调用类中引用头文件哦。。忘记说了。更新一下。。这里需要添加一个 GmaeKit.framework的框架引用。
over

 支持分享:)

mark 也许以后有用

学习了。。。。

— Begin quote from ____

老G 发表于 2012-6-21 09:32 url

精华了

— End quote

哈哈 感谢

怎么每次打开leaderboard再关闭,都检测出有内存泄露啊

2012-09-17 11:05:21.642 程序名省略 Authenticating local user…
2012-09-17 11:05:21.644 程序名省略 authenticateWithCompletionHandler: enter
2012-09-17 11:05:21.646 程序名省略 authenticateWithCompletionHandler: exit
在output有这样的提示但是运行的程序没有登录的提示,这是验件通过还是没通过腌?

会场有用,先做个标记

mark mark:14: