Chapter 4. Gathering Resources

In this chapter, we are going to gather and prepare the resources for Canyon Bunny to spice up the visual appearance of the game. We will do this using a collection of image files that have been created beforehand. You will learn how to replace the Android default launcher icon that comes with every new Android project. Additionally, you will be introduced to the technique of texture atlases and also learn how to create and use them in conjunction with LibGDX.

You will learn why it is important to keep track of your assets at runtime, and how to make it an almost hassle-free task by delegating most of it to LibGDX. Hassle-free means that you will not have to worry about keeping track when unloading or reloading your assets becomes necessary. Instead, LibGDX only needs to know what assets should be loaded to manage them transparently for you in the background.

Organizing the access to the loaded assets is another important topic. You will learn how to create your own Assets class that allows convenient and structured access from anywhere in the game code.

After this, you will put everything together. To verify that the images of the game objects are loaded and work just fine, you will replace the test sprite texture from Chapter 3, Configuring the Game, with a random selection of some of the loaded images.

Finally, you will learn how to handle the level data. This will enable you to create your own game world and populate it with game objects to your liking.

To sum up, in this chapter, we will:

  • Prepare the resources for Canyon Bunny
  • Organize the access to our resources in our code
  • Understand the level data
  • Run the game to test our resources

Setting up a custom Android application icon

First, we want to replace the default launcher icon that came with the generated Canyon Bunny Android project. There is a special directory called res that resides in the Android CanyonBunny-android project. It contains resource files that are exclusively available to the Android application.

You will see the following four folders starting with drawable in their names:

  • drawable-ldpi (low-density screen)
  • drawable-mdpi (medium-density screen)
  • drawable-hdpi (high-density screen)
  • drawable-xdpi (extra high-density screen)

These folders are used by Android to support different screen sizes and resolutions resulting in different screen densities. For the sake of simplicity, we will ignore the whole topic of screen support and create a special common folder called drawable. Android will use the contents from this folder regardless of the screen density detected.

The following screenshot shows the default launcher icon called ic_launcher.png that our application is currently using:

Setting up a custom Android application icon

Now, delete all the four files of the default launcher icon from drawable-ldpi, drawable-mdpi, drawable-hdpi, and drawable-xdpi.

The following image is what we want to use for the application icon of Canyon Bunny:

Setting up a custom Android application icon

Copy the new ic_launcher.png icon to the common drawable folder. As we have not changed the icon's name, it will work without any additional changes. Also, do not forget to change the icon's reference in the AndroidManifest.xml file if you want to rename it. The corresponding line should look like the following listing:

<application
    android:icon="@drawable/ic_launcher"
    ... />

The application icon is now replaced with our very own Canyon Bunny application icon. The following screenshot shows the installed application on Google Nexus 4:

Setting up a custom Android application icon

The support of different screens and resource sizes is a complex topic of its own, which is beyond the scope of this book. For more information, check out the official Android Developer websites at http://developer.android.com/guide/practices/screens_support.html and http://developer.android.com/design/style/iconography.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset