java - 使用Texturepacker/LibGDX 制作动画 - 渲染错误

标签 java android libgdx render texturepacker

我正在尝试使用带有 libGDX 的 Texturepacker 生成一个简单的 18 帧动画(其他信息:Eclipse、Windows 8)。

我的 Android 测试设备上出现以下 LogCat 错误,并且出现黑屏:

02-20 09:26:15.229: E/AndroidRuntime(23444): FATAL EXCEPTION: GLThread
02-20 09:26:15.229: E/AndroidRuntime(23444): java.lang.NullPointerException
02-20 09:26:15.229: E/AndroidRuntime(23444):    at com.tester.test.TestGameClass.render(TestGameClass.java:71)
02-20 09:26:15.229: E/AndroidRuntime(23444):    at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:499)
02-20 09:26:15.229: E/AndroidRuntime(23444):    at com.badlogic.gdx.backends.android.surfaceview.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
02-20 09:26:15.229: E/AndroidRuntime(23444):    at com.badlogic.gdx.backends.android.surfaceview.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)

LogCat 错误似乎指向我的游戏类的渲染部分:

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Array;

public class TestGameClass implements ApplicationListener {

AssetManager manager = new AssetManager();
Texture grey_background;
Texture tell_arrow;
Texture test_anni_1;
Music test_intro;
Music test_play;
Sound menu_select;
OrthographicCamera camera;
SpriteBatch batch;
TextureAtlas spriteSheet;
private Array<Sprite> test;
private int currentFrame;
private float frameLength;
private float animationElapsed;

@Override
   public void create () {

    Texture.setEnforcePotImages(false);
    grey_background = new Texture(Gdx.files.internal("grey_background.png"));
    tell_arrow = new Texture(Gdx.files.internal("tell_arrow.png"));
    test_intro = Gdx.audio.newMusic(Gdx.files.internal("test_intro.mp3"));
    test_play = Gdx.audio.newMusic(Gdx.files.internal("test_play.mp3"));
    menu_select = Gdx.audio.newSound(Gdx.files.internal("menu_select.wav"));

    test_intro.setLooping(true);
    test_intro.play();

     camera = new OrthographicCamera();
     camera.setToOrtho(false, 480, 640);

    spriteSheet = new TextureAtlas( Gdx.files.internal( "spritesheet.txt" ));
    test = spriteSheet.createSprites("test");

     for(int i=0; i<test.size; i++){
         test.get(i).setSize(3.0f, 3.0f);
     }

     float dt = Gdx.graphics.getDeltaTime();
     animationElapsed += dt;
     while(animationElapsed > frameLength){
         animationElapsed -= frameLength;
         currentFrame = (currentFrame == test.size - 1) ? 0 : ++currentFrame;
     }

    }

   @Override
   public void render() {
       Gdx.gl.glClearColor(0, 0, 0.2f, 1);
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

       camera.update();
       batch.begin();
       test.get(currentFrame).draw(batch);
       batch.end();
       }

   public void resize (int width, int height) { 
   }

   public void pause () { 
   }

   public void resume () {
   }

   public void dispose () { 
       grey_background.dispose();
       menu_select.dispose();
       test_intro.dispose();
       test_play.dispose();
       tell_arrow.dispose();
       batch.dispose();
   }
}

我的Texturepacker png图像和文本文档将每个测试帧称为test1、test2、test3等。

我之前收到错误,这些错误指向与 OpenGL 关联的行,以及分别指向与相机设置关联的行。我似乎已经解决了它们,但这也许是相关的?

谢谢。

最佳答案

您尚未初始化批处理。插入
batch = new SpriteBatch();
在你的 create() 方法中。
除此之外,你的代码
float dt = Gdx.graphics.getDeltaTime(); animationElapsed += dt; while(animationElapsed > frameLength){ animationElapsed -= frameLength; currentFrame = (currentFrame == test.size - 1) ? 0 : ++currentFrame; }
应该放在 render() 内部。

关于java - 使用Texturepacker/LibGDX 制作动画 - 渲染错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897064/

相关文章:

java - 如何扩展在其方法中返回 this 的类

java - 为 Java 和 .NET 实现单一数据访问层

java - 哪些编码问题可能会严重损害游戏的性能?

java - 奇怪的JAVA语法

java - Android、异步、回调和 UI

java - Libgdx 井字游戏

java - 一个 WebView 中的多个 URL

android - 如何在 recyclerview viewholder 中处理大量不同的 View 类型

java - 在 Objective c 中验证使用 PBKDF2WithHmacSHA1 算法在 Java 中加密的密码

java - 如何减慢box2d body 的线速度或角速度