0

我是 android dev 的新手,使用 andengine 和我在处理覆盖超过 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);

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

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

但该行返回 new TiledTextureRegion(pTexture, false, textureRegions); 期望 1 个纹理在创建平铺区域时从其中检索帧。我应该从这里开始的任何想法,或者是否有一种超级简单的方法来处理我已经忽略的这个问题。谢谢你的帮助

4

1 回答 1

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...
  }
于 2012-08-27T09:31:57.313 回答