1 package org.cocos2dx.lib;
2
3 public class Cocos2dxLuaJavaBridge
4 {
5 public static native int callLuaFunctionWithString(int luaFunctionId, String value);
6 public static native int callLuaGlobalFunctionWithString(String luaFunctionName, String value);
7 public static native int retainLuaFunction(int luaFunctionId);
8 public static native int releaseLuaFunction(int luaFunctionId);
9 }
public static native int callLuaFunctionWithString(int luaFunctionId, String value);
下面提供callLuaFunctionWithString用法:
function JniUtil:callJavaCallbackLua(func)
local luaj = require "cocos.cocos2d.luaj"
local className = "org/cocos2dx/lua/AppActivity"
local args = { "callbacklua", func }
local sigs = "(Ljava/lang/String;I)V"
local ok = luaj.callStaticMethod(className,"callbackLua",args,sigs)
if not ok then
print("============= call callback error")
else
print("------------- call callback success")
end
end
某启用充值的地方,预先注册局部函数
local function onBack(msg)
self:resumeGame()
end
JniUtil:getInstance():callJavaCallbackLua(onBack)
Java中可以直接回调这个函数,也可以先保存下来这个注册的Lua局部函数,一般都是保存下来,等待启用SDK充值逻辑走完后再回调
static int luaFuncCallback = 0;
public static void callbackLua(final String tipInfo,final int luaFunc){
// 根据需要,看是否要retainLuaFunction
luaFuncCallback = luaFunc;
// tipInfo为Lua里传来的“callbacklua”这里没用
}
Java中充值后回调函数中调用游戏里的lua逻辑
sInstance.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxLuaJavaBridge.callLuaFunctionWithString(luaFuncCallback, "success");
Cocos2dxLuaJavaBridge.releaseLuaFunction(luaFuncCallback);
}
});