- 先说下大体的思路。和之前说的在Unity中接入ASCSDK一样,在Cocos2dx中接入ASCSDK,我们也分为两步,一个Android中的部分,另 一个是Coccos2dx中的部分。同时,我们希望尽可能地封装不同平台之间数据的传递,以及接口的调用。使得大部分游戏基于这个封装,都可以最快地速度来完成ASCSDK的接入。
- 新建一个Android工程,作为ASCSDK和Cocos2dx的一个中间工程(ASCSDK_Cocos2dx),主要负责ASCSDK的接口调用封装,参数传递和解析等。这个工程,对于所有游戏都试用。
- 在Cocos2dx中封装JNI调用,以及和Android中交互的数据格式等的定义和解析。同样地,这部分对于所有游戏也都适用。游戏中只需要调用这里提供的相关接口,以及实现相应的回调即可完成接入。
- 首先, 我们使用cocos new 新建一个Cocos2dx(我这里使用的是Cocos2dx 3.10版本)工程,名为Cocos_Demo。
- 目录如下图
- demo中我们创建了5个按钮,分别为登陆,支付,评价,展示横幅,展示插屏。代码以及运行效果如下。
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto bg = Sprite::create("HelloWorld.jpg");
bg->setAnchorPoint(Vec2(0.5f, 0.5f));
bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
bg->setScale(2.0f);
this->addChild(bg);
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 - 5.0f ,
origin.y + closeItem->getContentSize().height/2 + 5.0f));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
this->labelUsername = Label::createWithTTF("ASCSDK", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
this->labelUsername->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height * 0.9 - this->labelUsername->getContentSize().height));
// add the label as a child to this layer
this->addChild(this->labelUsername, 1);
ui::Button* btnLogin = ui::Button::create("login_normal.png", "login_selected.png");
btnLogin->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y + 200));
btnLogin->addClickEventListener(CC_CALLBACK_1(HelloWorld::loginClickCallback, this));
btnLogin->setScale(0.6f);
// add the sprite as a child to this layer
this->addChild(btnLogin, 0);
ui::Button* btnPay = ui::Button::create("pay_normal.png", "pay_selected.png");
btnPay->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y + 100));
btnPay->addClickEventListener(CC_CALLBACK_1(HelloWorld::payClickCallback, this));
btnPay->setScale(0.6f);
// add the sprite as a child to this layer
this->addChild(btnPay, 0);
ui::Button* btnRate = ui::Button::create("rate_normal.png", "rate_selected.png");
btnRate->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
btnRate->addClickEventListener(CC_CALLBACK_1(HelloWorld::rateClickCallback, this));
btnRate->setScale(0.6f);
// add the sprite as a child to this layer
this->addChild(btnRate, 0);
ui::Button* btnBanner = ui::Button::create("banner_normal.png", "banner_selected.png");
btnBanner->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y - 100));
btnBanner->addClickEventListener(CC_CALLBACK_1(HelloWorld::showBannerClickCallback, this));
btnBanner->setScale(0.6f);
// add the sprite as a child to this layer
this->addChild(btnBanner, 0);
ui::Button* btnInters = ui::Button::create("inters_normal.png", "inters_selected.png");
btnInters->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y - 200));
btnInters->addClickEventListener(CC_CALLBACK_1(HelloWorld::showIntersClickCallback, this));
btnInters->setScale(0.6f);
// add the sprite as a child to this layer
this->addChild(btnInters, 0);
//NDKHelper::addSelector("ASCSelectors", "onASCLoginResult", CC_CALLBACK_2(HelloWorld::onASCLoginResult, this), this);
ASCSDKInterface::getInstance()->initSDK(this);
return true;
}
void HelloWorld::OnInitSuc()
{
CCLOG("SDK初始化成功...");
//一般不需要做什么处理
}
void HelloWorld::OnLoginSuc(ASCLoginResult* result)
{
CCLOG("SDK登录并认证成功...%s",result->userId.c_str());
this->labelUsername->setString(result->sdkUsername);
if (result->isSwitchAccount)
{
//TODO:这里退回到登录界面,并且SDK是已经登录成功状态,不用再弹出SDK的登录界面
}
else
{
//TODO:登录认证成功,获取服务器列表,进入游戏服...
}
}
void HelloWorld::OnSwitchLogin()
{
CCLOG("切换帐号成功...");
//TODO:退回到登录界面,并弹出SDK登录界面(调用ASCSDKInterface::getInstance()->login())
}
void HelloWorld::OnLogout()
{
CCLOG("SDK帐号退出成功...");
this->labelUsername->setString("ASCSDK");
//TODO:退回到登录界面,并弹出SDK登录界面(调用ASCSDKInterface::getInstance()->login())
}
void HelloWorld::loginClickCallback(Ref *pSender)
{
CCLOG("Btn Login Clicked...ASC Login...");
ASCSDKInterface::getInstance()->login();
}
void HelloWorld::payClickCallback(Ref *pSender)
{
CCLOG("Btn Pay Clicked...ASC Pay...");
ASCPayParams* params = ASCPayParams::create();
params->productId = "1";
params->productName = "元宝";
params->productDesc = "购买100元宝,赠送20元宝";
params->price = 100;
params->buyNum = 1;
params->coinNum = 300;
params->serverId = "10";
params->serverName = "地狱之恋";
params->roleId = "asc_24532452";
params->roleName = "麻利麻利吼";
params->roleLevel = 15;
params->vip = "v15";
//去服务器获取游戏中商品的订单号以及扩展数据,这里测试
params->orderID = "345435634534";
params->extension = "test111";
ASCSDKInterface::getInstance()->pay(params);
}
void HelloWorld::rateClickCallback(cocos2d::Ref* pSender){
CCLOG("Btn rate Clicked...ASC Rate...");
ASCSDKInterface::getInstance()->rate();
}
void HelloWorld::showBannerClickCallback(cocos2d::Ref* pSender){
CCLOG("Btn showBanner Clicked...ASC showBanner...");
ASCSDKInterface::getInstance()->showBanner();
}
void HelloWorld::showIntersClickCallback(cocos2d::Ref* pSender){
CCLOG("Btn showInters Clicked...ASC showInters...");
ASCSDKInterface::getInstance()->showInters();
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
-
说明:
- HelloWorldScene.cpp中init方法的最后,我们调用ASCSDK的初始化方法:
ASCSDKInterface::getInstance()->initSDK(this);
- HelloWorldScene.cpp中init方法的最后,我们调用ASCSDK的初始化方法:
- 实现回调接口
void HelloWorld::OnInitSuc()
{
CCLOG("SDK初始化成功...");
//一般不需要做什么处理
}
void HelloWorld::OnLoginSuc(ASCLoginResult* result)
{
CCLOG("SDK登录并认证成功...%s",result->userId.c_str());
this->labelUsername->setString(result->sdkUsername);
if (result->isSwitchAccount)
{
//TODO:这里退回到登录界面,并且SDK是已经登录成功状态,不用再弹出SDK的登录界面
}
else
{
//TODO:登录认证成功,获取服务器列表,进入游戏服...
}
}
void HelloWorld::OnSwitchLogin()
{
CCLOG("切换帐号成功...");
//TODO:退回到登录界面,并弹出SDK登录界面(调用ASCSDKInterface::getInstance()->login())
}
void HelloWorld::OnLogout()
{
CCLOG("SDK帐号退出成功...");
this->labelUsername->setString("ASCSDK");
//TODO:退回到登录界面,并弹出SDK登录界面(调用ASCSDKInterface::getInstance()->login())
}
- 登陆以及支付以及广告调用,回调方法,具体调用,请参考demo。
3.将Cocos_Demo生成proj.android导入到eclipse中,然后将ASCSDK3和ASCSDK_Cocos2dx工程生成的jar包(分别是ascsdk2.jar和ascsdk_cocos2dx.jar)拷贝到HelloCocos安卓工程的libs目录下以及一些外部jar。然后,将生成的 AppActivity.java,本来是继承Cocos2dxActivity的,改为继承ASCCocosActivity。
同时,将AndroidManifest.xml中的application节点中,加上android:name=”com.ascsdk.ASCApplication”
- 目录如图
- AndroidManifest.xml内容
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.asc.sdk.demo.cocos"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="19"/>
<uses-feature android:glEsVersion="0x00020000" />
<application android:label="@string/app_name"
android:icon="@drawable/icon"
android:name="com.asc.sdk.ASCApplication" >
<!-- Tell Cocos2dxActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="cocos2dcpp"/>
<activity android:name=".AppActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<supports-screens android:anyDensity="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
- 最后,为了能够在Android中编译并运行,我们还需要将ASCSDK包含的这些Cpp文件,添加到Android.mk中,最终的文件如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp \
../../Classes/jansson/value.c \
../../Classes/jansson/utf.c \
../../Classes/jansson/strconv.c \
../../Classes/jansson/strbuffer.c \
../../Classes/jansson/pack_unpack.c \
../../Classes/jansson/memory.c \
../../Classes/jansson/load.c \
../../Classes/jansson/hashtable.c \
../../Classes/jansson/error.c \
../../Classes/jansson/dump.c \
../../Classes/NDKHelper/CallFuncNV.cpp \
../../Classes/NDKHelper/NDKCallbackNode.cpp \
../../Classes/NDKHelper/NDKHelper.cpp \
../../Classes/ascsdk/ASCSDKData.cpp \
../../Classes/ascsdk/ASCSDKNativeCallback.cpp \
../../Classes/ascsdk/ASCSDKInterface.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
4.到这里,接入就算完成了。我们可以使用cocos compile -p android 命令来编译Android平台,之前的操作如果稍有不慎,这里编译的过程中,也许会遇到一些错误,不过,看着啥错误,一一解决即可。