TextureRegion getKeyFrame Libgdx方法错误



Libgdx错误显示内联57Exception in thread "LWJGL Application" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;帮助我如何将动画转换为TextureRegion idk如何获得它。我使用libgdx旧版本从MasteringLibGDXGameDevelopment_CodeBundle复制了它,并且没有从官方网站获得更新。我使用动画而不是TextureRegion,因为更新感谢帮助我…

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package RpgGame;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import java.util.Hashtable;
public abstract class GraphicsComponent implements Component {
protected TextureRegion _currentFrame;
protected float _frameTime = 0f;
protected Entity.State _currentState;
protected Entity.Direction _currentDirection;
protected Json _json;
protected Vector2 _currentPosition;
protected Hashtable<Entity.AnimationType, Animation<TextureRegion> > _animations;
protected ShapeRenderer _shapeRenderer;
protected GraphicsComponent(){
_currentPosition = new Vector2(0,0);
_currentState = Entity.State.WALKING;
_currentDirection = Entity.Direction.DOWN;
_json = new Json();
_animations = new Hashtable<Entity.AnimationType, Animation<TextureRegion> >();
_shapeRenderer = new ShapeRenderer();
_currentFrame =  null;
}
public abstract void update(Entity entity, MapManager mapManager, Batch batch, float delta);
protected void updateAnimations(float delta){
_frameTime = (_frameTime + delta)%5; //Want to avoid overflow
//Look into the appropriate variable when changing position
switch (_currentDirection) {
case DOWN:
if (_currentState == Entity.State.WALKING) {
Animation<TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_DOWN);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
} else if(_currentState == Entity.State.IDLE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_DOWN);
if( animation == null ) return;
_currentFrame = animation.getKeyFrames()[0];
} else if(_currentState == Entity.State.IMMOBILE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.IMMOBILE);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
}
break;
case LEFT:
if (_currentState == Entity.State.WALKING) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_LEFT);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
} else if(_currentState == Entity.State.IDLE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_LEFT);
if( animation == null ) return;
_currentFrame = animation.getKeyFrames()[0];
} else if(_currentState == Entity.State.IMMOBILE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.IMMOBILE);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
}
break;
case UP:
if (_currentState == Entity.State.WALKING) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_UP);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
} else if(_currentState == Entity.State.IDLE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_UP);
if( animation == null ) return;
_currentFrame = animation.getKeyFrames()[0];
} else if(_currentState == Entity.State.IMMOBILE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.IMMOBILE);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
}
break;
case RIGHT:
if (_currentState == Entity.State.WALKING) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_RIGHT);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
} else if(_currentState == Entity.State.IDLE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.WALK_RIGHT);
if( animation == null ) return;
_currentFrame = animation.getKeyFrames()[0];
} else if(_currentState == Entity.State.IMMOBILE) {
Animation <TextureRegion> animation;
animation = _animations.get(Entity.AnimationType.IMMOBILE);
if( animation == null ) return;
_currentFrame = animation.getKeyFrame(_frameTime);
}
break;
default:
break;
}
}
//Specific to two frame animations where each frame is stored in a separate texture
protected Animation loadAnimation(String firstTexture, String secondTexture, Array<GridPoint2> points, float frameDuration){
Utility.loadTextureAsset(firstTexture);
Texture texture1 = Utility.getTextureAsset(firstTexture);
Utility.loadTextureAsset(secondTexture);
Texture texture2 = Utility.getTextureAsset(secondTexture);
TextureRegion[][] texture1Frames = TextureRegion.split(texture1, Entity.FRAME_WIDTH, Entity.FRAME_HEIGHT);
TextureRegion[][] texture2Frames = TextureRegion.split(texture2, Entity.FRAME_WIDTH, Entity.FRAME_HEIGHT);
Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(2);
GridPoint2 point = points.first();
animationKeyFrames.add(texture1Frames[point.x][point.y]);
animationKeyFrames.add(texture2Frames[point.x][point.y]);
return new Animation(frameDuration, animationKeyFrames, Animation.PlayMode.LOOP);
}
protected Animation loadAnimation(String textureName, Array<GridPoint2> points, float frameDuration){
Utility.loadTextureAsset(textureName);
Texture texture = Utility.getTextureAsset(textureName);
TextureRegion[][] textureFrames = TextureRegion.split(texture, Entity.FRAME_WIDTH, Entity.FRAME_HEIGHT);
Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(points.size);
for( GridPoint2 point : points){
animationKeyFrames.add(textureFrames[point.x][point.y]);
}
return new Animation(frameDuration, animationKeyFrames, Animation.PlayMode.LOOP);
}
}

该代码来自MasteringLibGDXGameDevelopment_CodeBundle

如果您检查Javadoc中的animation.getKeyFrames(),您会看到它返回一个Object[],除非您用"类型感知数组"实例化您的Animation。这意味着当您在Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(2)行实例化Array时,还需要将类型传递给构造函数,因此将其更改为

Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(true, 2, TextureRegion.class);

相关内容

最新更新