我创建了一个项目并使用了动画。在我看来,代码是正常的,但在运行应用程序时会失败。
图片:
代码:
Texture texture2;
SpriteBatch batch;
float e=0;
Animation ani;
TextureRegion[] tr;
TextureRegion[][] tr2;
int index,i,j;
@Override
public void create()
{
try{
texture2 = new Texture(Gdx.files.internal("logo.png"));}
catch(Exception ex){}
batch = new SpriteBatch();
tr=new TextureRegion[2];
tr2=TextureRegion.split(texture2,94,18);
ani=new Animation(1f/4f,tr);
index=0;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
tr[index++]=tr2[j][i];
}
}
}
@Override
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
e+=Gdx.graphics.getDeltaTime();
batch.begin();
batch.draw(ani.getKeyFrame(e),0,0);
/*batch.draw(texture, Gdx.graphics.getWidth() / 4, 0,
Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);*/
batch.end();
}
日志:
FATAL EXCEPTION: GLThread 2510
Process: com.mycompany.mygame4, PID: 21018
java.lang.IllegalArgumentException: texture cannot be null.
at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java:37)
at com.badlogic.gdx.graphics.g2d.TextureRegion.split(TextureRegion.java:276)
at com.mycompany.mygame4.MyGdxGame.create(MyGdxGame.java:25)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:236)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)
请帮我弄清楚出了什么问题。