libgdxTextureRegion 类中出现异常

问题描述 投票:0回答:1

我正在按照教程使用 java 中的 libgdx 库创建一个类似口袋妖怪的游戏。 由于动画上的 libgdx 更新,我一直坚持制作行走动画部分,因此每次显示时都应将动画设置为TextureRegion(如果我没有记错的话)。

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

这是 getSprite 方法,游戏在尝试移动后崩溃(它做了一个小移动,但在结束之前崩溃了),在返回 walkAnimation.getKeyFrame(walkTimer); 时出现此异常线路:

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

变量动画设置如下:

 private AnimationSet<TextureRegion> animations;
和 AnimationSet 类:

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

终于在这里调用了:

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

我尝试在调试器上查看 getWalking(faceing) 和 walkTimer 值,但这部分似乎没有任何内容为空或错误。 另外,在我上传的版本中,我在返回之前投射了动画,以确保返回的对象是动画而不是对象。 没有强制转换也会发生同样的异常。错误可能出在 GameScreen 类的某个地方吗?

java libgdx game-development
1个回答
0
投票

找到答案了,在新的GameScreen中,动画应该设置为

new Animation<TextureRegion>
,并且
atlas.findRegion
应该替换为
atlas.findRegions
。这解决了问题并且运动正常工作。 最后 GameScreen 类的正确代码是:

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

© www.soinside.com 2019 - 2024. All rights reserved.