来自多个精灵表的动画精灵

时间:2012-08-27 09:14:06

标签: android andengine

我是Android dev的新手,并且使用andengine和ive在处理覆盖超过1个精灵表的大型动画时遇到了问题。基本上我有一个大的精灵,其动画跨越2个精灵表。我试图找到一种方法来成功加载它们。我会告诉你我在尝试什么,希望有人可以告诉我正确的方法,或者帮我完成它。

我首先从xml文件创建2个纹理包。 这些都很好

TexturePackTextureRegionLibrary packer1 = null,packer2 = null;
        TexturePack spritesheetTexturePack1 = null,spritesheetTexturePack2 = null;
        try {
            spritesheetTexturePack1 = new TexturePackLoader(activity.getTextureManager(), "Animation/Jack/")
                    .loadFromAsset(activity.getAssets(), "Jack_walk1" + ".xml");
            spritesheetTexturePack1.loadTexture();
            packer1 = spritesheetTexturePack1.getTexturePackTextureRegionLibrary();
        } catch (final TexturePackParseException e) {
            Debug.e(e);
        }

        TexturePackerTextureRegion textureRegion = packer1.get(Jack_walk1.LOOP_JACK_WALK_TO_SAFE_AREA_00000_ID);

        try {
            spritesheetTexturePack2 = new TexturePackLoader(activity.getTextureManager(), "Animation/Jack/")
                    .loadFromAsset(activity.getAssets(), "Jack_walk2" + ".xml");
            spritesheetTexturePack2.loadTexture();
            packer2 = spritesheetTexturePack2.getTexturePackTextureRegionLibrary();
        } catch (final TexturePackParseException e) {
            Debug.e(e);
        }

        TexturePackerTextureRegion textureRegion2 = packer2.get(Jack_walk1.LOOP_JACK_WALK_TO_SAFE_AREA_00000_ID);


        ArrayList<SparseArray> animList = new ArrayList<SparseArray>();
        animList.add(packer1.getIDMapping());
        animList.add(packer2.getIDMapping());
        TiledTextureRegion text1 = TiledTextureRegion.create(textureRegion.getTexture(), (int) textureRegion.getTextureX(),  (int) textureRegion.getTextureY(), animList);

然后我将此函数添加到tilestextureregion,以获取包含帧信息的数组列表,并逐步将它们添加到itexturregion数组

public static TiledTextureRegion create(final ITexture pTexture, final int pTextureX, final int pTextureY, final ArrayList<SparseArray> animList) {
        ITextureRegion[] textureRegions = null;
        int maxFrame = 0;

        for(int i = 0; i < animList.size(); i++){
            maxFrame += animList.get(i).size();
        }
        int currentFrame = 0;
        textureRegions = new ITextureRegion[maxFrame];
        for(int i = 0 ; i < animList.size(); i++){
            SparseArray<? extends ITexturePackTextureRegion> packer = animList.get(i);
            for(int j = 0; j < packer.size(); j++) {
                if (packer.valueAt(j)!= null){

                    final int x = (int) packer.valueAt(j).getTextureX();
                    final int y = (int) packer.valueAt(j).getTextureY();
                    final int width = packer.valueAt(j).getSourceWidth();
                    final int height = packer.valueAt(j).getSourceHeight();
                    final Boolean rotated = packer.valueAt(j).isRotated();


                    textureRegions[currentFrame] = new TextureRegion(pTexture, x, y, width, height, rotated);

                    currentFrame++;
                }
            }
        }


        return new TiledTextureRegion(pTexture, false, textureRegions);
    }

但该行返回新的TiledTextureRegion(pTexture,false,textureRegions);期待1个纹理在创建平铺区域时检索帧。我应该从这里出发的任何想法,或者有一个超级简单的方法来处理我已经看过。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

此类旨在使用单个纹理。

如果您无法将纹理合并为一个(我认为是这种情况),那么您可以尝试编写一个实现ITextureRegion的新类,该类将包含2个或更多TiledTextureRegion对象,并且将有一个方法来选择其中一个这些随意。 您只需将ITextureRegion的其余方法委托给所选对象。

public class MultipleTextureRegion implements ITextureRegion {
  private List<TiledTextureRegion> internal;
  private int selected=0;

  //...

  public void add(TiledTextureRegion region) {
    internal.add(region);
  }

  public void select(int index) {
    selected=index;
  }

  //...

  // Delegates all ITextureRegion methods ...

  public int getWidth() { return internal.get(selected).getWidth(); }
  // And so on...
  }