UmengHelper.lua 5.06 KB
cc.exports.UmengHelper = class("UmengHelper")  -- 构建lua 类

--调用安卓方法
local function callAndroidFun(funName,params,callBack)
    local luaj = require "cocos.cocos2d.luaj" --引入luaj
    --包名/类名  这个可以在对应的android工程的manifest中得到
    local className="org/cocos2dx/lua/UmengSDK";
    local args = { params,callBack};
    local sigs = "(Ljava/lang/String;I)V" --传入string参数,无返回值

    --luaj 调用 Java 方法时,可能会出现各种错误,因此 luaj 提供了一种机制让 Lua 调用代码可以确定 Java 方法是否成功调用。
    --luaj.callStaticMethod() 会返回两个值
    --当成功时,第一个值为 true,第二个值是 Java 方法的返回值(如果有)
    --当失败时,第一个值为 false,第二个值是错误代码
    local ok,ret = luaj.callStaticMethod(className,funName,args,sigs)
    if not ok then
        print("umeng "..funName.." error:"..ret)
    end
end

--调用IOS方法
local function callIosFun(funName,params,callBack)
    
end

function UmengHelper.onProfileSignIn(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("onProfileSignIn",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("onProfileSignIn",params,callBack);
    end
end

function UmengHelper.onProfileSignOff(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("onProfileSignOff",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("onProfileSignOff",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.startLevel(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("startLevel",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("startLevel",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.failLevel(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("failLevel",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("failLevel",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.finishLevel(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("finishLevel",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("finishLevel",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.pay(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("pay",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("pay",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.buy(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("buy",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("buy",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.use(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("use",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("use",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.bonus(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("bonus",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("bonus",params,callBack);
    end
end

--调用原生的方法实现
function UmengHelper.onEvent(params,callBack)
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if targetPlatform == cc.PLATFORM_OS_ANDROID then
        callAndroidFun("onEvent",params,callBack);
    elseif targetPlatform == cc.PLATFORM_OS_IPHONE or targetPlatform == cc.PLATFORM_OS_IPAD then
        callIosFun("onEvent",params,callBack);
    end
end

return UmengHelper