In this article I would like to show how to create new libgdx project and to share some useful tips on how to start a 2D game project.
!In this tutorial I will describe libgdx project for Android, some of the stuff won’t be shiftable to desktop/html 5 projects!
First of all we need to download some things. They are described in one of my previous posts: here
When you have your IDE and SDK configured and libgdx downloaded, you are ready to create your project.
All you have to do now is to run the gdx-setup-ui.jar file located in your downloaded libgdx project. The setup window will appear:
Now in the section labeled as 1:
Chose your project name, package name, game class and destination. They all can be as you like (except game class name have to start with a capital letter).
If you just want to make Android project like me, just uncheck „Generate the desktop/html/iOS project”.
In the section labeled as 2 you have two choices. You can either chose Stable version of libgdx or Nightlies version. Now Stable version is just normal libgdx that will work fine for you, while the Nightlies build has some new features, but they might have some minor errors so you are using it at your own risk. I rather use Stable version so let’s click the box with green arrow and the letter „S”. Setup will download the latest Stable libgdx build for you.
In the section labeled as 3 you can see what files will be created for you. You don’t have to do anything here.
In the last section labeled as 4 all you have to do is to click „Open the generation screen” button.
In the new windows just click the „Launch!” button.
There is small link at the bottom called „How do I import the projects into Eclipse” and there is everything clearly explained in 4 steps, so I won’t be writing that here again 🙂
When you have your game project loaded into Eclipse…nothing works. Of course, it never does 🙂
In my case the problems was because I chose (or didn’t) wrong Android version as a target (android-15 in my case). If you happen to have the same error, just run Android SDK Manager from your Android SDK folder and check and install your target platform. After you download everything you need. Go to YourGame-Android: Properties->Android and check your Target Name. All errors would be gone by now.
Before the first build (if you don’t have any Android device connected to your PC) you need to create new Virtual Device. Open Android Virtual Device Manager (AVD) from you Android SDK directory and create it with „New” button. Try to experiment with the settings and create more than one device. It will help you test your game with different conditions.
Assets folder should contain all external data (like sprites, music, fonts) you want to use in your game. But in libgdx it is located in YourGame-Android project and our code „lives” inside YourGame project so if we want to use our assets Eclipse will scream in pain with errors.
There is a simple „fix” for that. Just:
– Click the Source tab in Eclipse, click Link Source, Browse, select the „assets” folder from your Android project and click OK.
– Specify „assets” for the folder name and click Finish then OK.
Although many programmers consider it as an anti pattern I think a good game Singleton (If you don’t know abut Singletons, please take your time to read THIS) is necessary. Usually if game doesn’t take very much memory I keep all my textures loaded in my GameSingleton so they don’t have to load every time I change the screen. It just takes some more time to load them before the first screen appears.
Sometimes when you build the project it doesn’t get refreshed so every time before the build use Project->Clean… option
If you have problems with garbage collector or memory allocation use DDMS (Window->Open perspective->DDMS) there is „Allocation Tracker” tab that will help you find out what’s going on with your memory.
Always have LogCat (Window->Show view->Other…->Android->LogCat) opened while testing your app. It will show you what’s going on „beneath” your app.