Kahlua is an open-source Lua interpreter that can be embedded in j2s. It supports most Lua syntaxes, but some syntaxes are not supported.
In PC game development, Lua and Python are two of the most popular scripting languages, while Lua is more popular with simplicity and efficiency. At the same time, the simple syntax is also easy for game planning to quickly master, so as to quickly start the game plot writing.
Kahlua provides the possibility to use Lua scripts in mobile games.
The script engine for playing games using Kahlua is mainly used to define some function libraries that can be called by Lua in the Java program. Other things related to script syntax are handed over to Kahlua.
I tried to use Kahlua to write a simple example to implement a simple function: Move an image from the top left corner (0, 0) to the (100,100) position for display.
Paste the code first:
1)
Gamecanvas is defined in gamemidlet:
Import javax. microedition. lcdui. display;
Import javax. microedition. MIDlet. MIDlet;
Import javax. microedition. MIDlet. midletstatechangeexception;
Public class gamemidlet extends MIDlet {
Public static gamecanvas;
Public gamemidlet (){
Global. gamemidlet = this;
Gamecanvas = new gamecanvas ();
Display. getdisplay (this). setcurrent (gamecanvas );
}
@ Override
Protected void destroyapp (Boolean arg0) throws midletstatechangeexception {
}
@ Override
Protected void pauseapp (){
}
@ Override
Protected void Startapp () throws midletstatechangeexception {
}
}
2)
In gamecanvas, create an image display and define a function for Kahlua to set the image position (x, y)
Import java. Io. ioexception;
Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. graphics;
Import javax. microedition. lcdui. image;
Import CN. cocogame. mhdt. Script. gamescript;
Import CN. cocogame. mhdt. tool. tool;
Public class gamecanvas extends canvas implements runnable {
Image testsprite;
Public int x = 0; // The default image position is (0, 0)
Public int y = 0;
Graphics G1;
Public gamecanvas (){
Global. gamecanvas = this;
Setfullscreenmode (true );
Try {
Testsprite = image. createimage ("/critical.png"); // create an image
} Catch (ioexception e ){
E. printstacktrace ();
}
Global. gamescript = new gamescript (); // initializes the game script
Thread t = new thread (this );
T. Start ();
Global. gamescript. runscript ("/Lua/testlua"); // executes the game script testlua, where the function in gamescript is called.
}
@ Override
Protected void paint (Graphics g ){
G. setcolor (255,255,255 );
G. fillrect (0, 0,240,320 );
G. drawimage (testsprite, X, Y, graphics. Top | graphics. Left); // draw the image to the screen
}
@ Override
Public void run (){
While (true ){
// X = ++ X % 240;
// Y = + Y % 320;
System. Out. println ("x =" + x + "Y =" + y );
Repaint ();
Servicerepaints ();
Try {
Thread. Sleep (80 );
} Catch (exception e ){
}
}
}
3) Script Function Definition
Import se. krka. Kahlua. stdlib. baselib;
Import se. krka. Kahlua. VM. javafunction;
Import se. krka. Kahlua. VM. luacallframe;
Import se. krka. Kahlua. VM. luaclosure;
Import se. krka. Kahlua. VM. luastate;
Public class gamescript {
Private luastate state;
Public gamescript (){
Global. gamescript = this;
State = new luastate (system. Out );
// Set a bunch of functions
State. getenvironment (). rawset ("setposition", new javafunctionsetposition ());
}
/** Execute the script
* @ Param scriptname
*/
Public void runscript (string scriptname ){
Luaclosure callback = state. loadbytecodefromresource (scriptname, state. getenvironment ());
State. Call (callback, null );
}
// The following is the script implementation ============================================ ======================================
// Setposition ------------------------------------------------------------
Class javafunctionsetposition implements javafunction {
@ Override
Public int call (luacallframe callframe, int narguments ){
Int X1 = (baselib. rawtonumber (callframe. Get (0). intvalue ();
Int Y1 = (baselib. rawtonumber (callframe. Get (1). intvalue ();
String response = setposition (x1, Y1 );
Return 1;
}
}
Private string setposition (INT X1, int Y1 ){
Global. gamecanvas. x = x1;
Global. gamecanvas. Y = Y1;
Return NULL;
}
4)
Lua script
The Lua script needs to write such a sentence to implement the setposition function.
Local response = setposition (100,100 );
Then compile the Lua script into testlua. LBC using luac and put it in the project.
This is very convenient for planners.
5) Related Resources
Other Lua Resolvers include mochalua, but it seems that the jar package is larger than Kahlua.
Http://code.google.com/p/kahlua/
Mochalua URL: http://code.google.com/p/mochalua/