- Learning Libgdx Game Development
- Andreas Oehlke
- 896字
- 2021-08-06 16:38:53
Creating a new application
The next step is to create a new application. Usually, you would have to create several projects in Eclipse: one project for the shared game code, another one for the desktop launcher, and two more for the Android and HTML5/GWT launchers. Furthermore, the projects would also have to be configured and linked together in a certain way. This is quite a time-consuming and more or less an error-prone process for inexperienced users.
Luckily, Libgdx comes with a nice tool called Libgdx Project Setup, which can do all of the things described previously. It is able to generate preconfigured projects for a new application, which can be directly imported into Eclipse.
To run the tool, open the Windows Explorer, go to C:\libgdx\
, and double-click on the gdx-setup-ui
file. When the program starts, click on the big Create button as shown in the following screenshot:
In the next window, you will see a box called CONFIGURATION on the left-hand side. Here you can configure what the tool will generate.
Enter demo
in the Name field, which defines a common project name for your application. Each launcher project will add its own suffix to it, such as -desktop
, -android
, or -html
. A preview of the outcome is shown in the OVERVIEW box on the right-hand side.
The Package field defines the name of your Java package. This needs to be a unique identifier written in lowercase, which is usually derived from a reversed domain name. You do not have to own a domain name nor does it have to really exist, but it helps in choosing non-conflicting namespaces for Java applications. This is especially important on Android, as identical package names for two separate applications would mean that the already installed application is going to be overwritten by the second one while trying to install it. For this demo application, use com.packtpub.libgdx.demo
as the package name for now.
The Game class field defines the name of the main class in the shared game code project. Enter MyDemo
as the game class name.
The Destination field defines the destination folder where all the projects will be generated. Click on the blue folder button and set the destination folder to C:\libgdx\
.
In another box called LIBRARY SELECTION, the status of required libraries is shown. If there is any item listed in red color, it needs to be fixed before any project can be generated. You should see LibGDX being listed in red in the Required section. Click on the blue folder icon next to it as shown in the following screenshot:
Then, choose the downloaded archive file libgdx-0.9.7.zip
from C:\libgdx\
and click on Open, as shown in the following screenshot:
The text color of the LibGDX label should have changed from red to green by now. Click on the Open the generation screen button to continue as shown in the following screenshot:
Click on the Launch! button to generate all projects as shown in the following screenshot:
All done! You can now go to Eclipse and start importing the generated projects into your workspace. To do this, simply navigate to File | Import, as shown in the following screenshot:
In the Import dialog, open the General category, select Existing Projects into Workspace, and click on the Next > button as shown in the following screenshot:
Click on the radio button Select root directory and enter C:\libgdx
into the text field. This is the directory where all your generated projects were created. You need to confirm your text input by pressing the return key once. Eclipse will start to scan the directory for projects and list them. Leave all checkboxes selected and click on the Finish button, as shown in the following screenshot:
Eclipse will automatically try to build (compile) all four newly imported projects in your workspace and probably fail. There are two issues that need to be resolved manually after the import. The first one is reported directly to the Console window in Eclipse. It complains about being unable to resolve the target android-15.
You have to open the project properties of the demo-android
project. First, select it in the Package Explorer box on the left-hand side. Then go to the menu bar and navigate to Project | Properties , as shown in the following screenshot:
The title of the window should say Properties for demo-android. If this is not the case, close the window and make sure you have selected the correct project and try again. Next, select Android from the list on the left-hand side. You will see a list of targets that are available on your system. Select Android 2.2, which uses API Level 8, and click on the OK Button.
Eclipse should recognize the change and successfully build the Android project this time.
The second issue requires you to click on the Problems tab in Eclipse. Open the Errors list and right-click on the reported problem, which should say The GWT SDK JAR gwt-servlet.jar is missing in the WEB-INF/lib directory. Choose Quick Fix from the context menu as shown in the following screenshot:
In the Quick Fix dialog, select Synchronize <WAR>/WEB-INF/lib with SDK libraries as the desired fix and click on the Finish button, as shown in the following screenshot:
The two issues should be solved by now, which means all projects now are automatically built without failure and can be compiled.