zjweiss
zjweiss

Reputation: 41

Java does not cast over to TextureRegion

I am trying to draw multiple animations using HashMaps to switch between them. This is part of the code I am using.

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

When I run this code I get an error at that line. The error follows.

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

EDIT:

Here is the decloration for the hash maps:

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

Upvotes: 2

Views: 724

Answers (1)

KookieMonster
KookieMonster

Reputation: 503

It sounds like you have a HashMap of Array Lists, and each List contains the keyframes for your animation. If so, then what you are doing wrong, is treating the entire ArrayList like a single TextureRegion, rather than grabbing a single keyframe out of that ArrayList.

i.e;

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

Upvotes: 1

Related Questions