|
ProjectSetup
Note: Java 1.7 is currently problematic in combination with Android. Please make sure your project uses Java 1.6 Note: There's one more step to setup your Android project, see last step below This page describes how to configure Eclipse to develop and run libgdx applications on both the desktop and Android. Main project setup
Note: Step 5 makes gdx.jar a transitive dependency. That means that projects that depend on the main project will also have gdx.jar on their classpath. However, this doesn't work for Android projects. Desktop project setup
Android project setupBefore doing these steps, the Android SDK must first be installed.
Note: The folder really must be called "libs", a naming convention forced on us by the Android Eclipse plugin. Asset folder setupThe Android project has a subfolder named assets, which was created automatically. Files available to the Android application must be placed here. This is problematic, because these same files must be available to the desktop application. Rather than maintain two copies of all the files, the desktop project can be configured to find the assets in the Android project:
Note: If your desktop and Android projects are in the same parent folder, you can use "PARENT-1-PROJECT_LOC/gamename-android/assets" for the location of the linked assets folder, where "gamename-android" is the name of your Android project. This is better than a hardcoded path if you plan on sharing your projects with others. Creating a gameIn your main project, create a new class: right click the project -> New -> Class. Name it "Game" and specify a package (eg, "com.gamename"). Next to Interfaces, click Add, choose ApplicationListener, and click OK. It will look like this: 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 setup and render your game. Since they are empty, this particular game is simply a blank screen. We will first get this simple game running before doing something more interesting. Running the game on the desktopRight click the desktop project -> New -> Class. Name it DesktopGame and specify a package (eg, "com.gamename"). Click OK. Make the class look like this: 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 creates an LwjglApplication, gives it an instance of your game, a title, and size. The "false" means we aren't using OpenGL ES 2.0 (we will use 1.0/1.1). To run the game on the desktop, right click the project -> Debug As -> Java Application. You should get a black window titled "Game". Running the game on AndroidOpen the AndroidGame class in the Android project that was automatically created and make it look like this: 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);
}
}This code is an Android activity that calls initialize, passing an instance of your game. Again, the "false" means we aren't using OpenGL ES 2.0. To run the game on Android, right click the project -> Debug As -> Android Application. The screen will turn black, since the game doesn't yet do anything. If any errors occur running the application, they show up in the Logcat view, which can be seen by clicking Window -> Show View -> Other -> Android -> Logcat. Updating libgdxAt some point you may want to update libgdx to the latest. To do this, download the nightly zip again and update these files in your projects:
| |||||||||