本文主要是介绍cocos2d-x c++ 和 java互调,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我用了中国移动基地的sdk。
里面有so文件,但是编译的时候这个so文件总会被eclipse清空。
在编译之后再把so文件拷贝到libs目录下就可以打进apk了。
如果还不成功请移步:cocos2dx 中 Android NDK 加载动态库的问题
下面就是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); }
这篇关于cocos2d-x c++ 和 java互调的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!