cocos2d-x 3.7 C++ 接入Game Center

现在越来越多的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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值