Libgdx error show inline 57 that show
Exception in thread "LWJGL Application" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;
Help me how to convert Animation to TextureRegion idk how to get it. I copied from MasteringLibGDXGameDevelopment_CodeBundle it using libgdx old version and don't get an update from an official website I use Animation rather than TextureRegion because of update thanks for helping me....
/*
* 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);
}
}
this code from MasteringLibGDXGameDevelopment_CodeBundle
If you check the Javadoc for animation.getKeyFrames()
, you'll see that it returns an Object[]
unless you instantiate your Animation
with a "type-aware Array". This means when you instantiate the Array
on your line Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(2)
, you need to also pass the type to the constructor, so change it to
Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(true, 2, TextureRegion.class);