Java不会转换为TextureRegion

时间:2016-04-19 05:18:20

标签: java libgdx sprite-sheet

我正在尝试使用HashMaps绘制多个动画以在它们之间切换。这是我正在使用的代码的一部分。

    batch.draw((TextureRegion)hashmap.get(whichHashMap).get(c),x,y);

当我运行此代码时,我在该行收到错误。错误如下。

Error:(162, 54) java: incompatible types: java.util.ArrayList cannot be converted to com.badlogic.gdx.graphics.g2d.TextureRegion

编辑:

以下是哈希映射的decloration:

    ArrayList<ArrayList> whichHashMap;
    ArrayList<TextureRegion> animation1;
    ArrayList<TextureRegion> animation2;


    animation1 = new ArrayList<TextureRegion>();
    animation2 = new ArrayList<TextureRegion>();
    whichHashMap.add(animation1);
    whichHashMap.add(animation2);




    region = new TextureRegion(powerpuff,1,55,41,44);
    animation1.add(region);

    region = new TextureRegion(powerpuff,47,54,41,45);
    animation1.add(region);

    region = new TextureRegion(powerpuff,1,55,41,44);
    animation2.add(region);

    region = new TextureRegion(powerpuff,47,54,41,45);
    animation2.add(region);

1 个答案:

答案 0 :(得分:1)

听起来你有一个数组列表的HashMap,每个List都包含动画的关键帧。如果是这样,那么你做错了就是将整个ArrayList视为一个TextureRegion,而不是从该ArrayList中抓取一个关键帧。

即;

//the map is made like this;
Map<Object, ArrayList<TextureRegion>> hashMap = new HashMap<...>();

//hashmap.get(whichHashMap) returns an ArrayList<TextureRegion>
ArrayList<TextureRegion> animation = hashmap.get(animationID);

//now draw the correct frame
batch.draw(animation.get(frameNumber), x, y);