cocos2d-x Game Center
来源:互联网 发布:pano2vr.js 编辑:程序博客网 时间:2024/06/05 11:27
原文出处:http://www.cnblogs.com/jingjingWang/archive/2012/06/07/cocos2d-x.html
写这篇文章的主要用于给自己加强印象,帮助他人就是帮助自己.
本教程基于cocos2d-x 13版本编写 主要内容是告诉大家在c++中如何添加 game center ,找了很多网络上面的代码基本上都是缺胳膊少腿的,代码不完整,对于初学者来说是痛苦的。
本代码仅提供如何登陆GameCenter 显示和关闭Leaderboard(排行榜)。其他实现请参考子龙山人的博客,顺便贴下地址
http://www.cnblogs.com/zilongshanren/archive/2011/06/24/2088383.html 他讲解了很多东西。如何激活GameCenter等值得学习的文章。写的很细致哦废话不说上贴上代码咯我这里主要如何使用UIViewController 来实现GameCenter的呈现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// 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 = [[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;
}
}
好了。这个来说下如何调用它们吧。
先把AppDelegate.cpp 后缀修改为mm
在AppDelegate.mm里引入
#import "GameKitHelper.h"
在 applicationDidFinishLaunching方法里调用,代码写在运行场景之前即可
//GameCenter登陆
[[GameKitHelper sharedGameKitHelper] authenticateLocalUser];
到此登陆就ok啦。后面来教大家如何显示排行榜界面
//显示排行榜
[[GameKitHelper sharedGameKitHelper] showLeaderboard];
只要把此代码写在你想要调用的方法中就可以了。别忘记了在调用类中引用头文件哦。。
忘记说了。更新一下。。这里需要添加一个 GmaeKit.framework的框架引用(属性 Build Phases->Link Binary With Libraries add GameKit.framework)。
- cocos2d-x Game Center
- Cocos2d-X 添加 Game Center 的方法
- Cocos2d-X 添加 Game Center 的方法
- cocos2d-x 3.7 C++ 接入Game Center
- cocos2d-x 3.7 C++ 接入Game Center 使用Socket进行网络数据请求
- Game Center
- 关于COCOS2D加入Game center屏幕翻转的问题
- 『Game』Cocos2D-x学习
- Cocos2d-x Game Development Blueprints学习
- game+center+demo
- Game Center简述
- Game Center Overview
- 2dx Game Center
- App ID & Game Center - I
- 2dx game center 简介
- ios Game-Center 开发 part1
- ios Game-Center 开发 part2
- [Terminal Game Center]弹球游戏
- 备份
- VIM使用图示
- 备份2
- 备份3
- 清理oracle日志文件,防止硬盘空间占满
- cocos2d-x Game Center
- 细说Linux内核中断架构
- sql server 2008 不允许保存更改,您所做的更改要求删除并重新创建以下表 的解决办法
- 备份5
- 使用CCS连接DM6467 DSP端
- Oracle10g获取sql语句的执行计划详解!
- ASP.NET MVC3 部署的前期工作
- 备份6
- SQL多表查询优化 高效率SQL语句 11条原则