Game Development Reference
In-Depth Information
The next step is to retrieve our subimages from the loaded texture atlas. In general,
this is done by calling the atlas's findRegion() method, which takes the asset's name
as an argument. The method returns an object of the AtlasRegion class that contains
information about the subimage found and also some additional information about
how it is stored in the atlas.
Now, for example, if we wanted to find a subimage that is stored in assets/my_
image.png , we will write the following code:
atlas.findRegion("my_image");
Note that the prefix assets/ is always omitted just like any file extension, such as
.png . However, the subfolder needs to be specified. The method will silently return
null if the lookup fails. Therefore, be sure to double-check your spelling of the
filename in this case. One very important fact to know about this method is that the
lookup of a subimage is an expensive function call.
Using atlas.findRegion() in render() will affect performance;
so, it is highly recommended to cache the results after the initial
lookup to avoid severe performance issues.
Add the following line of code to Assets to import the AtlasRegion class:
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
The following is a step-by-step implementation of several smaller inner classes of the
Assets class. These classes allow us to structure (or group) the subimages in logical
units and also to permanently store (that is, cache) the looked up references.
We will begin with the player character and will call its class AssetBunny . It contains
a member variable called head that holds the reference to the texture atlas subimage
that originates from the bunny_head.png file. The lookup is done inside the
constructor of the inner class. The constructor takes a reference of the corresponding
atlas in which it will find the atlas region it wants. You can see the bunny head in the
following screenshot:
 
Search WWH ::




Custom Search