【问题标题】:animated sprite from texturepacker xml来自texturepacker xml的动画精灵
【发布时间】:2012-08-16 00:36:03
【问题描述】:

一般来说是 android dev 和 andengine 的新手。尝试使用 AndEngineTexturePackerExtension 为精灵设置动画,但我不确定如何为动画精灵创建 tiledTextureRegion。以下是我从本论坛的指南和其他帖子中获得的我正在尝试的内容。我从 texturepacker 创建 xml、png 和 java

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());

问题是我不明白 COLUMNS 和 ROWS 的值来自哪里?精灵表本身具有不均匀的行和列,因为它包括旋转的精灵等。所以我对这些值的来源感到困惑。任何有关使这项工作的帮助都将非常感谢

编辑:好的,如果我只使用纹理打包器中的基本算法而不是 MaxRects 算法,我可以让精灵表动画工作。但这并没有利用工作表中的所有空间,所以我宁愿使用 MaxRects 生成的精灵表让它工作。我在 xml 中看到它传递了一个 bool 来进行旋转或不旋转,所以有信息可以使这项工作我无法弄清楚如何。当一些纹理在工作表上旋转时,我如何使用纹理打包器纹理区域制作动画精灵

【问题讨论】:

  • 我也遇到过这个问题。我禁用了 Trim 选项并使用基本算法而不是 maxrects,现在很好。

标签: android andengine texturepacker


【解决方案1】:

TexturePacker 不知道您的精灵有多少列和行,即使它们是否平铺也不知道,它只是将所有内容打包到一个精灵表中(例如 png 文件)。此外,它的目标不是从分离的 Sprites 创建 TiledSprites。

因此,为了从 spritesheet 中取回 TiledSprite(或 AnimatedSprite),您必须知道在将其放入spritesheet,因为 TexturePacker 不会给你那种信息。

我个人使用的 TextureRegionFactory 看起来像这样:

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);
    }
}

编辑:关于旋转的问题,TexturePacker生成的xml里面有写,所以可以调用

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

然后,您可以根据该值创建 tiledTextureRegion:

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

另外,我希望你清楚 TexturePacker 并不是用来创建平铺精灵的。您必须在使用 TexturePacker之前创建平铺精灵(非常适合或行和列)。

【讨论】:

  • 但是 texturepacker 不会在行和列中提供 spritesheet,因为它会旋转一些图像以使其更适合给定页面。所以对我来说,即使是硬编码一个值也是错误的,因为工作表不是以这种方式构造的
  • 等等,你的意思是我必须想象一下在texturepacker 得到它之前这张表会是什么样子?并以这种方式计算行和列?
  • 我使用免费版的TexturePacker,所以我的精灵不会旋转,我不知道这些信息是否写在生成的xml中。我上面的意思是你在生成的 xml 中拥有关于你的精灵的所有信息(位置、宽度/高度..),但是你没有每个精灵的行/列。因此,如果您打包具有 8 列和 2 行的爆炸平铺精灵,它不会写入您的 xml。当您从纹理打包器创建爆炸动画精灵时,您必须手动指示此爆炸是 8 列、2 行的平铺精灵...
  • 是的,我明白了,这就是我的问题所在,我的一些图像被旋转了,所以它们不适合很好的行和列排列。这该死的令人沮丧
  • 我想我爱你!!!它应该处理旋转的精灵,感谢您为我清理它,我在这里发疯了!只有最后一件事。说动画没有填充我传入的行数和列数的最后一行。目前它会显示空白,因为它试图在工作表中显示空白区域。无论如何?
猜你喜欢
  • 1970-01-01
  • 2011-12-01
  • 2013-01-17
  • 2011-08-30
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多