java - Animation libGDX issue -


i created first animation class calls animation_ss. make class take code from: https://github.com/libgdx/libgdx/wiki/2d-animation. transformed code site class. dont know why after run program it's closed. use libgdx lib. here console output

exception in thread "lwjgl application" java.lang.nullpointerexception @ animationdemo.animationdemo.render(animationdemo.java:78) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication.mainloop(lwjglapplication.java:215) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication$1.run(lwjglapplication.java:120) 

it shows render method.

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

and here animation_ss class

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

i guess forgetting initialize spritebatch object, don't see line somewhere in code:

batch = new spritebatch(); 

so when call batch.begin(); in render() method npe because object not created.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -