Lua learning 2) -- interaction between Android and Lua, androidlua

Source: Internet
Author: User

Lua learning 2) -- interaction between Android and Lua, androidlua

2014-07-09

1. Android class calls lua and calls back

Android calls the Lua function, transmits the class as a parameter, and then calls back the class function in Lua.

Call lua

MLuaState = LuaStateFactory. newLuaState (); mLuaState. openLibs (); mLuaState. ldoString (KKLua. loadAssetsString (mContext, "lua/swallow. lua "); // converts lua to the string mLuaState. getField (LuaState. LUA_GLOBALSINDEX, "luaUpdate"); // obtain functionmLuaState in lua. pushJavaObject (this); // pass the mLuaState parameter. call (1, 0); // a parameter with 0 returned values

Callback Interface

Public void luaDraw (int imageId, int x, int y, int scale) {// note that it must be public before lua can be called to System. out. println ("imageId, x, y, scale:" + imageId + "," + x + "," + y + "," + scale );}
Lua code
function luaUpdate(luaCanvas)  luaCanvas:luaDraw(0, 1, 2, 1)end

II. Introduction to luajava ( Included in the LuaJava Library)

There is no class in lua. The luajava library has a luajava class that can be created and called in java. The following describes several methods:

① NewInstance (className ,...)

Note: You can create a Java class based on the class name and return a lua variable corresponding to the Java class. In this way, you can directly call methods in Java in lua.

obj = luajava.newInstance("java.lang.Object")-- obj is now a reference to the new object-- created and any of its methods can be accessed.-- this creates a string tokenizer to the "a,b,c,d"-- string using "," as the token separator.strTk = luajava.newInstance("java.util.StringTokenizer",     "a,b,c,d", ",")while strTk:hasMoreTokens() do    print(strTk:nextToken())end

② BindClass (className)

Note: You can set variables in lua to correspond to a Java class (a class, not an instance). In this way, you can use the variables in lua to create instances and call static classes.

sys = luajava.bindClass("java.lang.System")print ( sys:currentTimeMillis() )-- this prints the time returned by the function.

③ New (javaClass)

Note: This is created on the basis of ②.

str = luajava.bindClass("java.lang.String")strInstance = luajava.new(str)


④ CreateProxy (interfaceNames, luaObject)

⑤ LoadLib (className, methodName)

These two methods are not used yet, and will be used later

Thank you!


Teach you a question about porting android to the lua project in cocos2d-x

Build_native.sh




I have been studying androidfor more than two months. Now I want to learn the android source code, but which of the following classes should I look at first?

First look at activityManager, which is the process for managing the entire activity from startup to destruction!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.