cocos2d-x c++ 和 java互调

来源:互联网 发布:软件质量计划 编辑:程序博客网 时间:2024/06/05 07:01

我用了中国移动基地的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);}
                                             
0 0