This article is a translation of the first tutorial for the libGDX project. The original is -
here.From the translator: I recently had a desire to write a game for Android. I have some programming experience in Java, but I almost never came across Android development. Here I came across the libGDX library, which allows you to write cross-platform games. I also came across that there is not enough Russian documentation for it, and specifically - tutorials. Therefore, I had a desire to translate tutorials. This is my first translation, so I will welcome any comments and indications of inaccuracies.
Project Setup
Note: There are problems with using Java 1.7 for Android applications. Make sure you are using Java 1.6
Note: you need to take a few steps to configure the application for Android. See below.
')
This article describes how to set up Eclipse to develop and run applications on both the desktop and Android devices.Main project setup
1. Create a new Java project in Eclipse.
2. Navigate to the project directory in the file system and create a directory in it called libs. Download
nightly zip (nightly zip is an archive from the latest build of the project - note of the translator) and put gdx.jar and gdx-sourses.jar in the libs / directory
3. Right-click the project in Eclipse, and click Refresh. Right-click again, select Properties -> Java Build Path -> Libraries -> Add JARs, select jdx.jar and click OK.
4. Right-click on the file gdx.jar -> Properties -> Java source attachment. Click Workspace, select gdx-sources.jar and click OK.
5. Click the Order and Export tab, check the box for gdx.jar and click OK.
Note: the fifth step will make gdx.jar transitively dependent. This means that projects that depend on the main project will also have gdx.jar in their classpath. However, this does not apply to Android applications.
Customize the desktop application.
1. Create a new project in Eclipse. Name it appropriately (gamename-desktop, for example).
2. Navigate to the project directory in the file system and create a subdirectory in it called libs. Put the gdx-natives.jar, gdx-backend-lwjgl.jar, gdx-backend-lwjgl-natives.jar libraries into the libs directory.
3. Right-click on the project, click Refresh. Right-click again -> Properties -> Java Build Path -> Libraries -> Add JARs, select three libraries from the libs directory and click OK.
4. Go to the Projects tab, click Add, select the main project created above and click OK.
Setting up an application for Android.
Before you take the next steps, you need to make sure that you have the Android SDK installed.1. Create a new Android project in Eclipse. Give it a suitable name (for example, gamename-android). For Android, select Android 1.5 or higher. Specify the name of the package (for example, com.gamename). Then click on “Create Activity” and enter “AndroidGame”. Click Finish.
2. Navigate to the project directory in the file system and create a subdirectory libs in it. Put in it the gdx-backend-android-jar library and the armeabi and armeabi-v7a directories from the nightly zip.
3. In Eclipse, right-click on the project, click Refresh. Right-click the project again -> Properties -> Java Build Path -> Libraries -> Add JARs, select jdx-backend-android.jar and click OK.
4. Click Add JARs again, select jdx.jar from the main project and click OK.
5. Go to the Projects tab, click Add, select the main project and click OK.
6. Go to the Order and Export tab and tick the main project.
Note: the libs subdirectory should have a strictly such name. This is due to the naming conventions adopted in the Android Eclipse Plugin.
Setting up the assets directory.
Android projects have a subdirectory called assets, which is created automatically. Files that will be available to the Android application must be placed there. This creates a problem, since the same files must be accessible to the desktop application. It is better to configure the desktop application so that it can find these files in the assets directory than to maintain two copies of the files. To do this, follow these steps:
1. Go to the project properties for the desktop, select Java Build Path, go to the Source tab and click on Link Source -> Browse, select the assets directory from your Android application and click OK.
2. Specify the name of the assets in the Folder Name field, click Finish, and then OK.
Note: If your desktop and Android apps are in the same parent directory, you can use “PARENT-1-PROJECT_LOC / gamename-android / assets” to specify the linked assets folder, where gamename-android is the name of your Android project. This is better than hard-coded if you are going to distribute your projects.
Creating a game.
At your main project, create a new class. Name it Game and give it a package name (com.gamename, for example). In the Interfaces field, click Add, select ApplicationListener b click OK. You should have something similar:
import com.badlogic.gdx.ApplicationListener; public class Game implements ApplicationListener { public void create () { } public void render () { } public void resize (int width, int height) { } public void pause () { } public void resume () { } public void dispose () { } }
These methods allow you to customize your game and control its rendering. While they are empty, this game will not display anything - just a blank screen. We need to make this game run before we do something more interesting.
Running a game for the desktop application
Right-click on the desktop application and create a new class. Call it DesktopGame and specify a package for it (for example, com.gamename). Click OK. Bring this class to the following form:
import com.badlogic.gdx.backends.lwjgl.LwjglApplication; public class DesktopGame { public static void main (String[] args) { new LwjglApplication(new Game(), "Game", 480, 320, false); } }
This code will create an instance of LwjglApplication, give it a copy of your game, with the specified name and window size. The last parameter false indicates that you do not need to use OpenGL ES 2.0 (we will use 1.0 / 1.1).
To start the application, right-click on the project -> Debug as -> Java Application. Select the DesktopGame class and click OK. You will see a black window with the title Game. (It seems to me that it is easier to open the DesktopGame class and press Ctrl + F11 - comment of the translator).
Run the game on Android.
Open the AndroidGame class with your Android project, which should be created automatically and bring it to the following view:
import com.badlogic.gdx.backends.android.AndroidApplication; public class AndroidGame extends AndroidApplication { public void onCreate (android.os.Bundle savedInstanceState) { super.onCreate(savedInstanceState); initialize(new Game(), false); } }
The initialize method in activity (Activity) creates and runs an instance of our game. Again, false means that we do not use OpenGL ES 2.0
To run the game on Android, right-click on the project -> Debug As -> Android Application. The screen will be black, because our game is not doing anything. If there are any errors, they will appear in the Logcat view, which you can open by clicking on the Window menu -> Show View -> Other -> Android -> Logcat.
Libgdx update
If you want to update libgdx, download nigtly zip again and update the following files for your projects:
Main project: libs / gdx.jar, libs / gdx-sources.jar
Android: libs / gdx-backend-android.jar, libs / armeabi (directory), libs / armeabi-v7a (directory)
Desktop application: libs / gdx-natives.jar, libs / gdx-backend-lwjgl.jar, libs / gdx-backend-lwjgl-natives.jar