Search code examples
javalibgdxgame-development

Exception in libgdx TextureRegion class


I was following a tutorial to create a pokemon like game with libgdx library in java. I am stuck on making the walking animation part because of the libgdx update on Animation , so the animation should be set to TextureRegion each time displayed(if I am not mistaken ).

public TextureRegion getSprite() {
        if (state == ACTOR_STATE.WALKING) {
            Animation<TextureRegion> walkingAnimation = animations.getWalking(facing);
            return walkingAnimation.getKeyFrame(walkTimer);
        } else if (state == ACTOR_STATE.STANDING) {
            return animations.getStanding(facing);
        }
        return animations.getStanding(DIRECTION.SOUTH);
    }

This is the method getSprite and the game is crashing after trying to move ( it makes a small move but before ending it crashes ) with this exception on the return walkingAnimation.getKeyFrame(walkTimer); line:

Exception in thread "main" java.lang.ClassCastException: class com.badlogic.gdx.graphics.g2d.Animation$PlayMode cannot be cast to class com.badlogic.gdx.graphics.g2d.TextureRegion (com.badlogic.gdx.graphics.g2d.Animation$PlayMode and com.badlogic.gdx.graphics.g2d.TextureRegion are in unnamed module of loader 'app')

The variable animations is set like : private AnimationSet<TextureRegion> animations; and the AnimationSet class:

package util;

import com.badlogic.gdx.graphics.g2d.Animation;
import model.DIRECTION;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;



    public class AnimationSet<TextureRegion> {

        private Map<DIRECTION, Animation<TextureRegion>> walking;
        private Map<DIRECTION, TextureRegion> standing;

        public AnimationSet(Animation<TextureRegion> walkNorth,
                            Animation<TextureRegion> walkSouth,
                            Animation<TextureRegion> walkEast,
                            Animation<TextureRegion> walkWest,
                            TextureRegion standNorth,
                            TextureRegion standSouth,
                            TextureRegion standEast,
                            TextureRegion standWest ) {
            walking = new HashMap<DIRECTION, Animation<TextureRegion>>();
            walking.put(DIRECTION.NORTH, walkNorth);
            walking.put(DIRECTION.SOUTH, walkSouth);
            walking.put(DIRECTION.EAST, walkEast);
            walking.put(DIRECTION.WEST, walkWest);
            standing = new Hashtable<DIRECTION, TextureRegion>();
            standing.put(DIRECTION.NORTH, standNorth);
            standing.put(DIRECTION.SOUTH, standSouth);
            standing.put(DIRECTION.EAST, standEast);
            standing.put(DIRECTION.WEST, standWest);
        }

        public Animation<TextureRegion> getWalking(DIRECTION dir){
            return walking.get(dir);
        }

        public TextureRegion getStanding(DIRECTION dir){
            return standing.get(dir);
        }
    }

Finally it's called here:

 public GameScreen(MyGdxGame app) {
        super(app);
        south = new Texture("assets/stand.png");
        room1 = new Texture("assets/tile2.png");
        room2 = new Texture("assets/tile2.png");
        batch = new SpriteBatch();
        TextureAtlas atlas = app.getAssetManager().get("assets/packed/textures.atlas", TextureAtlas.class);
        AnimationSet animations = new AnimationSet(
                new Animation(0.3f/2f,atlas.findRegion("dawn_walk_north"), Animation.PlayMode.LOOP_PINGPONG),
                new Animation(0.3f/2f,atlas.findRegion("dawn_walk_south"), Animation.PlayMode.LOOP_PINGPONG),
                new Animation(0.3f/2f,atlas.findRegion("dawn_walk_east"), Animation.PlayMode.LOOP_PINGPONG),
                new Animation(0.3f/2f,atlas.findRegion("dawn_walk_west"), Animation.PlayMode.LOOP_PINGPONG),
                atlas.findRegion("dawn_stand_north"),
                atlas.findRegion("dawn_stand_south"),
                atlas.findRegion("dawn_stand_east"),
                atlas.findRegion("dawn_stand_west")
        );

        map = new TileMap(10,10);
        actor = new Actor(map,0,0,animations);
        controller = new PlayerController(actor);
        camera = new Camera();
    }

I've tried to see the getWalking(facing) and walkTimer values on debugger but nothing seems to be null or wrong with this part. Also in the version I uploaded i casted Animation before returning to make sure the returned object is Animation and not Object. Same exception happens without casting. Could the error be somewhere on the GameScreen class?


Solution

  • Found the answer , in the new GameScreen the animations should be set with new Animation<TextureRegion>, and atlas.findRegion should be replaced with atlas.findRegions. That solved the problem and the movement is working correctly. Finally the correct code for GameScreen class is:

    AnimationSet<TextureRegion> animations = new AnimationSet<TextureRegion>(
                    new Animation<TextureRegion>(0.3f/2f,atlas.findRegions("dawn_walk_north"), Animation.PlayMode.LOOP_PINGPONG),
                    new Animation<TextureRegion>(0.3f/2f,atlas.findRegions("dawn_walk_south"), Animation.PlayMode.LOOP_PINGPONG),
                    new Animation<TextureRegion>(0.3f/2f,atlas.findRegions("dawn_walk_east"), Animation.PlayMode.LOOP_PINGPONG),
                    new Animation<TextureRegion>(0.3f/2f,atlas.findRegions("dawn_walk_west"), Animation.PlayMode.LOOP_PINGPONG),
                    atlas.findRegion("dawn_stand_north"),
                    atlas.findRegion("dawn_stand_south"),
                    atlas.findRegion("dawn_stand_east"),
                    atlas.findRegion("dawn_stand_west")
            );