quick-cocos2d-x中Lua调用JAVA

前端之家收集整理的这篇文章主要介绍了quick-cocos2d-x中Lua调用JAVA前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 local btn
    btn = cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "call Java - showAlertDialog()",size = 64}))
            :onButtonClicked(function()
                if device.platform ~= "android" then
                   -- print("please run this on android device")
                    btn:setButtonLabel(cc.ui.UILabel.new({text = "please run this on android device",size = 32}))
                    return
                end
                
                -- call Java method
                local javaClassName = "org/cocos2dx/lua/AppActivity"
                local javaMethodName = "showAlertDialog"
                local javaParams = {
                    "How are you ?","I'm great !",function(event)
                        local str = "Java method callback value is [" .. event .. "]"
                        btn:setButtonLabel(cc.ui.UILabel.new({text = str,size = 32}))
                    end
                }
                local javaMethodSig = "(Ljava/lang/String;Ljava/lang/String;I)V"
                luaj.callStaticMethod(javaClassName,javaMethodName,javaParams,javaMethodSig)
            end)
            :align(display.CENTER,display.cx,display.cy)
            :addTo(self)

    btn:setKeypadEnabled(true)
    btn:addNodeEventListener(cc.KEYPAD_EVENT,function (event)
        dump(event)
        local str = "event.key is [ " .. event.key .. " ]"
                        btn:setButtonLabel(cc.ui.UILabel.new({text = str,size = 32}))
        if event.key == "back" then
            --print("back")
            cc.Director:getInstance():endToLua()
            if device.platform == "windows" or device.platform == "mac" then
                os.exit()
            end
        elseif event.key == "menu" then
            --print("menu")
        end
end)
没啥说的,就是给自己做个记录

猜你在找的Cocos2d-x相关文章