Let’s assume I have two images in a sprite sheet: A background and a foreground image. The foreground image is the same size as the background, but has a lot of transparent space around it.
What I would like, is to "trim" the foreground image into the sprite sheet (making it smaller) but to recompute its exact position when it is retrieved from the atlas. After creating the sprite sheet the xml looks like this:
<TextureAtlas imagePath="sheet.png"> <SubTexture name="background" x="2" y="2" width="1024" height="708"/> <SubTexture name="foreground " x="2" y="712" width="764" height="612" frameX="-257" frameY="-96" frameWidth="1024" frameHeight="708"/> </TextureAtlas>
There are two possibilities to work with the data of foreground:
- Resize the resulting image to the given frameWidth and frameHeight
- Leave the size as it is, but reposition the image to account for frameX and frameY
The first possibility is built into the TextureAtlas. This is nice in a lot of cases. The second approach can be useful as well though: In my case I want to preserve the position of foreground, but would like to make it touchable for user interaction. In this case I would like to keep the foreground image small, so I can distinguish it from other elements.
I would wish for the TextureAtlas, to be used in both ways. One approach would be to give an extra Parameter to getTexture, or to set the computation mode in the TextureAtlas constructor.
My workaround at the moment is:
- Create two xml files: One with (trim) and one without the frame-Attributes (crop).
- Use the xml without the frame-Attributes for the TextureAtlas
- After creating an image: Offset the position of the image by frameX and frameY read from the second xml.