3

new to android dev and andengine in general. trying to animate a sprite using the AndEngineTexturePackerExtension but im unsure how the tiledTextureRegion gets created for the animated sprite. below is what im trying which i have gotten from guides and other posts in this forum. Im creating the xml,png and java from texturepacker

private TexturePackTextureRegionLibrary mSpritesheetTexturePackTextureRegionLibrary;
private TexturePack texturePack;

 try
         {
                 TexturePackLoader texturePackLoader = new TexturePackLoader(activity.getTextureManager());
                 texturePack = texturePackLoader.loadFromAsset(activity.getAssets(), "spritesheet.xml");
                 texturePack.loadTexture();
                 mSpritesheetTexturePackTextureRegionLibrary = texturePack.getTexturePackTextureRegionLibrary();
         }
         catch (TexturePackParseException e)
         {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
         }

 TexturePackerTextureRegion textureRegion = mSpritesheetTexturePackTextureRegionLibrary.
                 get(spritesheet.00000_ID);

TiledTextureRegion tiledTextureRegion = TiledTextureRegion.create(texturePack.getTexture(),
                 textureRegion.getSourceX(), textureRegion.getSourceY(),
                 textureRegion.getSourceWidth() , textureRegion.getSourceHeight() ,
                 COLUMNS, ROWS);

AnimatedSprite sprite = new AnimatedSprite((activity.CAMERA_WIDTH - tiledTextureRegion.getWidth()) / 2,
                 (activity.CAMERA_HEIGHT - tiledTextureRegion.getHeight()) / 2,
                 tiledTextureRegion, activity.getVertexBufferObjectManager());

the problem is that i dont understand where the values from COLUMNS and ROWS comes from? the sprite sheet itself has uneven rows and columns as it includes rotated sprites etc. So im confused as to where these values come from. Any help on getting this working would be great thanks

edit: Ok i can get the sprite sheet animation working if i just use the basic algorithm within texture packer and not the MaxRects algorithm. But this doesnt make use of all the space within a sheet so i would rather get it working using a MaxRects generated sprite sheet. I see with in the xml that it pass a bool for being rotated or not so the information is there to make this work i just cant figure out how. how do i use a texturepackertexture region to make an animated sprite when some of the textures are rotated on the sheet

glogic
  • 4,042
  • 3
  • 28
  • 44
  • I have also had this issue. I disabled the Trim option and used the basic algorithm instead of maxrects and it is fine right now. – Mustafa Jul 13 '13 at 00:08

1 Answers1

4

TexturePacker doesn't know how much columns and rows have your sprites, not even if they are tiled or not, it just packs everything into a single spritesheet (png file for example). Also, its goal isn't to create TiledSprites from separated Sprites.

So, in order to get back a TiledSprite (or AnimatedSprite) from a spritesheet, you have to know how much columns and rows (it can be hardcoded somewhere) it had before being put into the spritesheet, since TexturePacker won't give you that kind of information.

I personally use a TextureRegionFactory which looks like this:

public class TexturesFactory {

    public static final String SPRITESHEET_DIR = "gfx/spritesheets/";
    public static final String TEXTURES_DIR = SPRITESHEET_DIR+"textures/";

    private TexturePack mTexturePack;
    private TexturePackTextureRegionLibrary mTextureRegionLibrary;

    /**
     * 
     * @param pEngine
     * @param pContext
     * @param filename
     */
    public void loadSpritesheet(Engine pEngine, Context pContext, String filename) {

        try {
            this.mTexturePack = new TexturePackLoader(
                    pEngine.getTextureManager(), TEXTURES_DIR).loadFromAsset(
                            pContext.getAssets(), filename);

            this.mTextureRegionLibrary = this.mTexturePack.getTexturePackTextureRegionLibrary();
            this.mTexturePack.getTexture().load();

        } catch (TexturePackParseException ex) {
            Log.e("Factory", ex.getMessage(), ex);
        }
    }

    public TextureRegion getRegion(int id) {
        return this.mTextureRegionLibrary.get(id);
    }

    public TiledTextureRegion getTiled(int id, final int rows, final int columns) {

        TexturePackerTextureRegion packedTextureRegion = this.mTextureRegionLibrary.get(id);

        return TiledTextureRegion.create(
                packedTextureRegion.getTexture(), 
                (int) packedTextureRegion.getTextureX(), 
                (int) packedTextureRegion.getTextureY(), 
                (int) packedTextureRegion.getWidth(), 
                (int) packedTextureRegion.getHeight(), 
                columns, 
                rows);
    }
}

Edit: About the rotated problem, it is written inside the xml generated by TexturePacker, so you can get it by calling

TexturePackerTextureRegion packedTextureRegion = this.mTextureRegionLibrary.get(id);
packedTextureRegion.isRotated()

Then, you can create the tiledTextureRegion according to that value with:

TiledTextureRegion.create(
                packedTextureRegion.getTexture(), 
                (int) packedTextureRegion.getTextureX(), 
                (int) packedTextureRegion.getTextureY(), 
                (int) packedTextureRegion.getWidth(), 
                (int) packedTextureRegion.getHeight(), 
                columns, 
                rows,
                packedTextureRegion.isRotated());

Also, I hope it is clear to you that TexturePacker isn't meant to create tiled sprites. You must create your tiled sprites (nice fit or rows and columns) before using TexturePacker.

Michel-F. Portzert
  • 1,785
  • 13
  • 16
  • but texturepacker doesnt give the spritesheet in rows and columns as it rotates some images to make more fit on a given page. so for me to even hard code a value would be wrong as the sheet is not structured in that way – glogic Aug 21 '12 at 07:07
  • wait do you mean i have to imagine how the sheet would look before texturepacker gets its hands on it ? and work out the rows and columns that way ? – glogic Aug 21 '12 at 07:11
  • I use the free version of TexturePacker so my sprites are not rotated, and I don't know if this information is written inside the generated xml or not. What i meant above is that you have all information about your sprites inside the generated xml (position, width/height..), but you don't have the rows/columns of each sprite. So if you pack an explosion tiled sprite that have 8 columns and 2 rows, it won't be written on your xml. When you create the explosion animated sprite from texture packer, you have to manually indicate this explosion is a tiled sprite of 8 columns, 2 rows... – Michel-F. Portzert Aug 21 '12 at 17:34
  • yeah i get you, thats where my problem is , some of my images are rotated so they dont fit in a nice rows and columns arrangement. its damn frustrating – glogic Aug 22 '12 at 08:36
  • i think i love you man!!! that handles the rotated sprites as it should, thanks for clearing that up for me i was going mad here! just 1 last thing. say the animation doesnt fill the last row of the rows and columns numbers that i pass in. currently it will display blank as it tries display the empty space from with in the sheet. anyway around that? – glogic Aug 23 '12 at 09:57
  • actually i got around it by making change to andengine that takes in number of frames and handles the loop that way. thanks for the help again so much appreciated! – glogic Aug 23 '12 at 10:15