Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top