TextureRegion getKeyFrame Libgdx 方法错误
TextureRegion getKeyFrame Libgdx method error
Libgdx error show inline 57 表示
Exception in thread "LWJGL Application" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;
帮助我如何将 Animation 转换为 TextureRegion idk 如何获取它。我使用 libgdx 旧版本从 MasteringLibGDXGameDevelopment_CodeBundle 复制它并且没有从官方网站获得更新 我使用 Animation 而不是 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
如果您查看 animation.getKeyFrames()
的 Javadoc,您将看到它 returns 一个 Object[]
,除非您用 "type-aware Array" 实例化您的 Animation
.这意味着当您实例化 Array
行 Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(2)
时,您还需要将类型传递给构造函数,因此将其更改为
Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(true, 2, TextureRegion.class);
Libgdx error show inline 57 表示
Exception in thread "LWJGL Application" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.graphics.g2d.TextureRegion;
帮助我如何将 Animation 转换为 TextureRegion idk 如何获取它。我使用 libgdx 旧版本从 MasteringLibGDXGameDevelopment_CodeBundle 复制它并且没有从官方网站获得更新 我使用 Animation 而不是 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
如果您查看 animation.getKeyFrames()
的 Javadoc,您将看到它 returns 一个 Object[]
,除非您用 "type-aware Array" 实例化您的 Animation
.这意味着当您实例化 Array
行 Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(2)
时,您还需要将类型传递给构造函数,因此将其更改为
Array<TextureRegion> animationKeyFrames = new Array<TextureRegion>(true, 2, TextureRegion.class);