cocos2d-x和java_cocos2d-x c++ 和 java互调

我用了中国移动基地的sdk。

里面有so文件,但是编译的时候这个so文件总会被eclipse清空。

在编译之后再把so文件拷贝到libs目录下就可以打进apk了。

下面就是jni的使用了。

部分java文件:

package com.qingxue.game;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.KeyEvent;

import android.widget.Toast;

import cn.cmgame.billing.api.BillingResult;

import cn.cmgame.billing.api.GameInterface;

import cn.cmgame.billing.api.GameInterface.GameExitCallback;

import cn.cmgame.billing.api.GameInterface.IPayCallback;

public class GunStreet extends Cocos2dxActivity

{

//单例

public static GunStreet instance = null;

protected void onCreate(Bundle savedInstanceState)

{

GameInterface.initializeApp(this);

//初始化

instance = GunStreet.this;

super.onCreate(savedInstanceState);

}

static

{

System.loadLibrary("game");

}

public static boolean isMusicPlay()

{

return GameInterface.isMusicEnabled();

}

public void showMoreGames()

{

GameInterface.viewMoreGames(getContext());

// GameInterface.ShowMoreGames();

}

//获取单例

public static Object getInstance()

{

Log.e("instance", "jobj create Already");

return instance;

}

//本地方法在c++中实现

private static native void getPoint(int gain);

//支付sdk 回调

final IPayCallback payCallbackOne = new IPayCallback()

{

public void onResult(int resultCode, String billingIndex, Object obj) {

String result = "";

switch (resultCode) {

case BillingResult.SUCCESS:

result = "购买道具:[" + billingIndex + "] 成功!";

break;

case BillingResult.FAILED:

result = "购买道具:[" + billingIndex + "] 失败!";

break;

default:

result = "购买道具:[" + billingIndex + "] 取消!";

getPoint(10000);

break;

}

Toast.makeText(GunStreet.this, result, Toast.LENGTH_SHORT).show();

}

};

//支付点

public void gainPointOne()

{

Log.e("gainPointOne", "gainPointOne is called");

GameInterface.doBilling(GunStreet.this, true, true, "001", null, payCallbackOne);

}

}

c++调用java

void ShopLayer::buyMoney_callBack(CCObject* pSender)

{

CCLog("buyMoney_callBack");

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台

JniMethodInfo minfo;

jobject jobj;

if (JniHelper::getStaticMethodInfo(minfo, "com/qingxue/game/GunStreet",

"getInstance", "()Ljava/lang/Object;"))

{

//获取单例

jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);

if (JniHelper::getMethodInfo(minfo, "com/qingxue/game/GunStreet",

"gainPointOne", "()V"))

{

CCLog("jobj start");

if(jobj == NULL)

{

return;

}

minfo.env->CallVoidMethod(jobj, minfo.methodID);

CCLog("jobj end");

}

}

#endif

}

c++实现java里面native方法:

//本地指针

static ShopLayer* shopLayer = NULL;

CCScene* ShopLayer::scene()

{

CCScene* sc = CCScene::create();

ShopLayer* layer = ShopLayer::create();

//初始化

shopLayer = layer;

sc->addChild(layer);

return sc;

}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台

extern "C"

{

void Java_com_qingxue_game_GunStreet_getPoint(JNIEnv* env, jobject thiz, jint a)

{

shopLayer->setGainPoint(a);

}

}

#endif

void ShopLayer::setGainPoint(int gain)

{

int coin = Config::sharedConfig()->getCoin();

Config::sharedConfig()->setCoin(coin+gain);

CCUserDefault::sharedUserDefault()->setIntegerForKey("coin",coin+gain);

((CCLabelAtlas*)getChildByTag(238))->setString(CCString::createWithFormat("%d",coin+gain)->getCString());

CCLog("%d",coin+gain);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值