现在越来越多的ios游戏都要接入game center了。不记得什么时候看过的一篇报道。貌似苹果规定,ios上的游戏,如果有涉及到用户的,必须接入game center。无论真假,总之game center我认为是一个大部分开发游戏的人都应该了解的东东。
说说这几天我遇到的困难。最困难的在于详细资料太少。说真的代码什么的网上确实有很多。各种各样。可是没有一个step by step的教程。让我们这些转行或者刚接触cocos2d-x开发的程序员很苦恼。
经过2天的折腾(主要被一个地方卡住了80%的时间。后面会说到),终于成功的实现了登陆game center账号,并且获取到playerID。
下面是我实现的过程。
一共需要这么几个类
AppDelegate.cpp改名AppDelegate.mm
HelloWorldScene.h
HelloWorldScene.mm
GameKitHelper.h
GameKitHelper.mm
其中GameKitHelper这个类是参考了网上的大部分写法。一个类,一个类说明
AppDelegate.mm
#include "AppDelegate.h"
#include "HelloWorldScene.h"
#import "GameKitHelper.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate()
{
}
//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
bool AppDelegate::applicationDidFinishLaunching() {
[[GameKitHelper sharedGameKitHelper] authenticateLocalUser];
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::createWithRect("GameCenterTest", cocos2d::Rect(0, 0, 640, 960));
director->setOpenGLView(glview);
}
director->getOpenGLView()->setDesignResolutionSize(640, 960, ResolutionPolicy::SHOW_ALL);
// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);
FileUtils::g