Spritesheets
There are two important concepts if you want to create a texture animation: spritesheets and atlases. Read both articles first before deciding which solution is most suitable for your game.
A spritesheet is a single image that contains multiple smaller images, called frames. Each frame represents a different version of a character, object, or visual element in a game.
For example, if you have a character walking, instead of saving six separate image files for each step, you can put all six frames in a single image — this is your spritesheet.
The image below shows you an example of a spritesheet:
There are many benefits of using spritesheets:
- It has a positive impact on the performance, since we only have to load a single image into memory (which is faster than loading many small images).
- All frames are stored in a single collection, so it is easier to organize.
- You can draw individual frames using a row and column system.
In the Saxion Game App, a spritesheet is handled as a grid of frames (rows × columns). Each frame is a TextureRegion that represents a piece of the spritesheet. You can draw any frame at any position on the screen using the provided methods.
Adding a Spritesheet
// For a 512x512 image with 6 columns and 5 rows:
GameApp.addSpriteSheet("heroWalk", "images/hero_walk.png", 85, 102);
- name: a unique name for your spritesheet (e.g., "heroWalk").
- filePath: the path to the image file inside your project.
- frameWidth and frameHeight: the width and height of a single frame in pixels.
Note: The image does not need to be a perfect square, and rows/columns can be any size. The frame size tells the program how to slice the image. It will automatically detect how many rows and columns there are. You just have to define the slice size.
Drawing Spritesheet Frames
Drawing spritesheet frames is very similar to drawing textures. You will have to call the GameApp.startSpriteRendering(); and end with GameApp.endSpriteRendering();.
You can draw frames from a spritesheet using row and column numbers. Rows and columns are 0-based (the first row/column is 0).
Draw at a position
Draw with scaling
Draw with rotation and flipping
- rotationDegrees: rotates the frame clockwise.
- flipX: mirror horizontally.
- flipY: mirror vertically.
Draw centered at a position
GameApp.drawSpritesheetFrameCentered("heroWalk", 400, 300); // Draws the frame centered at x=400, y=300
Similarly, there are also methods for drawing scaled, rotated and flipped from the center.
Useful spritesheet helpers
Check if a spritesheet exists
Get number of rows and columns
int rows = GameApp.getSpritesheetNumberOfRows("heroWalk"); // Returns 5
int cols = GameApp.getSpritesheetNumberOfCols("heroWalk"); // Returns 6
Remove a spritesheet and free memory
Always dispose of spritesheets when they are no longer needed to avoid using too much memory.
Example
The example below shows you how to load a spritesheets and render a couple of frames from it.
public class HeroScreen extends ScalableGameScreen {
...
@Override
public void show() {
// Load a spritesheet with 5 rows and 5 columns from a 500x500 image
GameApp.addSpriteSheet("heroWalk", "images/hero_walk.png", 100, 100);
}
@Override
public void render(float delta) {
super.render(delta);
GameApp.startSpriteRendering();
// Draw the first frame (row 0, column 0) at position x=100, y=100
GameApp.drawSpritesheetFrame("heroWalk", 0, 0, 100, 100);
// Draw a frame from row 1, column 2 at position x=200, y=150
GameApp.drawSpritesheetFrame("heroWalk", 1, 2, 200, 150);
// rotated 45 degrees clockwise, flipped horizontally but not vertically
GameApp.drawSpritesheetFrame("heroWalk", 2, 1, 500, 200, 100, 100, 45, true, false);
GameApp.endSpriteRendering();
}
@Override
public void hide() {
// Dispose the spritesheet to free memory
GameApp.disposeSpritesheet("heroWalk");
}
}