动画 libGDX 问题
Animation libGDX issue
我创建了我的第一个动画 class,它调用 Animation_SS。为了使这个 class 我从 https://github.com/libgdx/libgdx/wiki/2D-Animation 中获取代码。我将代码从这个站点转换到我的 class。我不知道为什么,但在 运行 程序后它立即关闭。我使用 libGDX 库。
这是控制台输出
Exception in thread "LWJGL Application" java.lang.NullPointerException
at AnimationDemo.AnimationDemo.render(AnimationDemo.java:78)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
显示渲染方法。
这是主要的class
public class AnimationDemo extends ApplicationAdapter {
Animation_SS walkGuy;
SpriteBatch batch;
@Override
public void create () {
walkGuy = new Animation_SS(6, 0.025f, true);
walkGuy.loadSS("StickSS.png");
walkGuy.cropSS(4, 2);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
walkGuy.loadCurrentFrame();
batch.begin();
walkGuy.drawFrame(batch,10,10);
batch.end();
}
}
这里是Animation_SSclass
int cols;
int rows;
int totalFrames;
float duration;
float stateTime=0f;
Boolean restart;
Texture texture;
TextureRegion[] frames;
TextureRegion currentFrame;
Animation myAnimation;
SpriteBatch batch;
public Animation_SS(int totalFrames, float duration, Boolean letItRestart){
this.totalFrames = totalFrames;
this.duration = duration;
this.restart = letItRestart;
}
public void loadSS(String path){
texture = new Texture(Gdx.files.internal(path));
}
public void cropSS(int FRAME_COLS, int FRAME_ROWS){
this.cols = FRAME_COLS;
this.rows = FRAME_ROWS;
TextureRegion[][]framesArray = TextureRegion.split(texture, texture.getWidth()/FRAME_COLS, texture.getHeight()/FRAME_ROWS);
frames = new TextureRegion[totalFrames];
int index = 0;
for (int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(index<totalFrames)
frames[index++] = framesArray[i][j];
}
}
myAnimation = new Animation(duration, frames);
}
public void loadCurrentFrame(){
stateTime +=1 *Gdx.graphics.getDeltaTime();
currentFrame = myAnimation.getKeyFrame(stateTime, restart);
}
public void drawFrame(SpriteBatch batch, float x, float y){
batch.draw(currentFrame,x ,y);
}
}
我猜你忘了初始化 SpriteBatch
对象,我在你的代码中没有看到这一行:
batch = new SpriteBatch();
因此,当您在 render()
方法中调用 batch.begin();
时,您将得到一个 NPE
,因为未创建此对象。
我创建了我的第一个动画 class,它调用 Animation_SS。为了使这个 class 我从 https://github.com/libgdx/libgdx/wiki/2D-Animation 中获取代码。我将代码从这个站点转换到我的 class。我不知道为什么,但在 运行 程序后它立即关闭。我使用 libGDX 库。 这是控制台输出
Exception in thread "LWJGL Application" java.lang.NullPointerException
at AnimationDemo.AnimationDemo.render(AnimationDemo.java:78)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
显示渲染方法。
这是主要的class
public class AnimationDemo extends ApplicationAdapter {
Animation_SS walkGuy;
SpriteBatch batch;
@Override
public void create () {
walkGuy = new Animation_SS(6, 0.025f, true);
walkGuy.loadSS("StickSS.png");
walkGuy.cropSS(4, 2);
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
walkGuy.loadCurrentFrame();
batch.begin();
walkGuy.drawFrame(batch,10,10);
batch.end();
}
}
这里是Animation_SSclass
int cols;
int rows;
int totalFrames;
float duration;
float stateTime=0f;
Boolean restart;
Texture texture;
TextureRegion[] frames;
TextureRegion currentFrame;
Animation myAnimation;
SpriteBatch batch;
public Animation_SS(int totalFrames, float duration, Boolean letItRestart){
this.totalFrames = totalFrames;
this.duration = duration;
this.restart = letItRestart;
}
public void loadSS(String path){
texture = new Texture(Gdx.files.internal(path));
}
public void cropSS(int FRAME_COLS, int FRAME_ROWS){
this.cols = FRAME_COLS;
this.rows = FRAME_ROWS;
TextureRegion[][]framesArray = TextureRegion.split(texture, texture.getWidth()/FRAME_COLS, texture.getHeight()/FRAME_ROWS);
frames = new TextureRegion[totalFrames];
int index = 0;
for (int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(index<totalFrames)
frames[index++] = framesArray[i][j];
}
}
myAnimation = new Animation(duration, frames);
}
public void loadCurrentFrame(){
stateTime +=1 *Gdx.graphics.getDeltaTime();
currentFrame = myAnimation.getKeyFrame(stateTime, restart);
}
public void drawFrame(SpriteBatch batch, float x, float y){
batch.draw(currentFrame,x ,y);
}
}
我猜你忘了初始化 SpriteBatch
对象,我在你的代码中没有看到这一行:
batch = new SpriteBatch();
因此,当您在 render()
方法中调用 batch.begin();
时,您将得到一个 NPE
,因为未创建此对象。