Book Image

Learning Libgdx Game Development

Book Image

Learning Libgdx Game Development

Overview of this book

Game development is a field of interdisciplinary skills, which also makes it a very complex topic in many respects. One decision that usually needs to be made at the beginning of a game development processis to define the kind of computer system or platform the game will be developed for. This does not pose any problems in general but as soon as the game should also be able to run on multiple platforms it will become a developer's nightmare to maintain several distinct copies of the same game. This is where the libGDX multi-platform game development framework comes to the rescue! "Learning Libgdx Game Development" is a practical, hands-on guide that provides you with all the information you need to know about the libGDX framework as well as game development in general so you can start developing your own games for multiple platforms. You will gradually acquire deeper knowledge of both, libGDX and game development while you work through twelve easy-to-follow chapters. "Learning Libgdx Game Development" will walk you through a complete game development cycle by creating an example game that is extended with new features over several chapters. These chapters handle specific topics such as organizing resources, managing game scenes and transitions, actors, a menu system, using an advanced physics engine and many more. The chapters are filled with screenshots and/or diagrams to facilitate comprehension. "Learning Libgdx Game Development" is the book for you if you want to learn how to write your game code once and run it on a multitude of platforms using libGDX.
Table of Contents (19 chapters)
Learning Libgdx Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.