The Game Engine LIME in Android Studio

LIME is a Game Engine for JavaScript.

Many games made for Android and other mobile devices are made in JavaScript.

Here is an example how the JavaScript Code that uses LIME can be integrated into an Android Project to use it in Android Studio for further development of the Android part.

roundball_AS_2

 

 

Make your own Candy-Crush-Like Game

 

 

 

USING THE WEBKIT

Start a new project with an Activity using the Blank Activity template for example and call it for exmple “Roundball”.

The Activity will use “web kit” to show the JavaScript content.

Android’s “Web kit” gives us the option of defining our own browser; the contents can, but must not, use the entire space in a layout.

We will first need to import a web kit in order to use it. We will need to add the WebView. Then create a variable for the WebView.

The layout area, where the contents need to be displayed, is set in a widget named WebView.
The web kit provides a series of ON – OFF buttons similar to JavaScript = true or false.
The game in this example is called “Roundball”, created in JavaScript and built on the “LIME” GameEngine.

A demo version of this game can be downloaded from the LIME’s website.

COPYING THE JAVASCRIPT INTO THE PROJECT

Web resources, which are to be stored locally in the app, are stored in a folder called assets/.

To create this folder, right click on the main/ folder, select -> New -> Directory and specify “assets”
as the folder name. Here you can later copy the HTML/ JavaScript files belonging to the game.

We will first need to import a web kit in order to use it. We will need to add the WebView. Then create a variable for the WebView to be created afresh.

package com.greendog.example.webapp;
import android.os.Bundle; import android.app.Activity;
importandroid.webkit.WebView;
public class Roundball extends Activity {
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roundball);

//The code for loading the game in the WebView is given below. R.id.webView1 does not exist at
//the moment. This becomes the ID that will add to the WebView in the activity_roundball layout
//file.

setDomStorageEnabled(true);

//Many JavaScript games like this one and most LIME games are built on DOM instead of
//Canvas. setJavaScriptEnabled(true); naturally, JavaScript has to be enabled for a JavaScript game.

loadUrl("file:///android_asset/roundball/roundball.html");

 

THE LAYOUT FILE

The layout file in this example is called “activity_roundball.xml”.
The layout file comprises a widget, the WebView and its ID is webView1 that is called from the
Java class.

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" /> </RelativeLayout>

That’s it. The App is ready to go.

The Source Code for the example can be found here.

Tags: