SpriteSheets – Textures – and Texture Regions


Sprite Sheets save fair amount of memory and CPU usage.  In my future world, I can also use them later to create different monsters, levels, etc. using the same graphics with different color pallets.  As I get closer to production code, I feel this is one of the last items I need to investigate.

I found two good examples on exactly how to use TextureMaps.  I suggest reviewing or viewing both websites, as I am not going to recreate the documentation wheel.

Games From Scratch Tutorials

Games from Scratch once again has an excellent post in both Video and html formats:

Video:

Pick the Creating and using Spritesheets with TextureAtlas.

Code and Webpage:

The textual version of the same demo is located here.

A Full Game Demo:

I found another tutorial that I thought were very helpful.  It is a complete game, I just was interested in the TextureAtlas, but it also touches on Animations and other game development.  The code is available on Github, and the game on GooglePlay.   If you want to check it out, click here.

My Use Case

My first use case is very simple.  I have a bubble game.  Let’s vary the color of the bubbles.  We will start with six (6) bubbles and put them into a single sprite sheet.  To do this, I download Texture Packer.  I was impressed with this because after the download, it asked the platform I used.  Once I picked libGDX, it sent me to a series of tutorials, and that was a nice.

The output was simple a png and a txt file that I simply uploaded to my assets directory in my JavaStudio project and used just like the demos showed.  There were two items of note that neither demo covered

1) If you want to use the free version of Texture Packer, you need to set the “Trim Mode” to “None” so that it completes all of the rendering.  Otherwise you will get red components in your final image.

TexturePacker

If you forget, your image will look something like this:

Bubbles_red_small

2) If you want to move a sprite, consider using translate method in the Sprite class.  According to the libGDX API it is more efficient.  There are methods for translating x, y independently or together.  The Sprite javadocs has all the info.  There may be more methods, but I used these three in my code:

sprite.translateX(-1f);
sprite.translateY(-1f);
sprite.translate(-1f, -1f);

Leave a comment

Your email address will not be published. Required fields are marked *