2014-02-20 44 views
0

我想用libGDX(其他信息:Eclipse,Windows 8)使用Texturepacker產生一個簡單的18幀動畫。Texturepacker/LibGDX動畫 - 渲染錯誤

我得到以下logcat的錯誤,再加上我的Android試驗裝置在黑色屏幕:

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等

我以前獲得該指着線assoc命令錯誤使用OpenGL,單獨使用與相機設置相關的線。我似乎已經解決了他們,但也許這是相關的?

謝謝。

+1

錯誤指向第71行,並解釋您有一個空指針。應該相當容易找出,但如果你需要額外的幫助,你可以考慮共享哪條線實際上是71 ...瘋狂猜測; test爲null或test.get(..)返回null。 –

+0

Hi Marius,第71行指向batch.begin(); – jdubbing

回答

1

你有havnt初始化批處理。在您的create()方法中插入
batch = new SpriteBatch();

除了這個,你的代碼
float dt = Gdx.graphics.getDeltaTime(); animationElapsed += dt; while(animationElapsed > frameLength){ animationElapsed -= frameLength; currentFrame = (currentFrame == test.size - 1) ? 0 : ++currentFrame; }
應放在render()。

+0

感謝Pranav-我不再在LogCat中發現任何錯誤。我仍然看不到屏幕上的動畫(單獨的問題)。我猜這可能是我的相機設置,但我似乎無法修復它。 – jdubbing

+1

使用batch.setProjectionMatrix(camera.combined);在render()中。其次你havnt初始化frameLength的任何價值。第三,你爲什麼不使用libgdx中提供的Animation類對象繪製動畫。如果您是libgdx中的新成員,並且沒有關於動畫類的信息,請查看http://code.google.com/p/libgdx/wiki/SpriteAnimation – Pranav008

+0

感謝Pranav。我在一小時前發現了您在鏈接中提到的教程(比我使用的其他教程更容易遵循)。我已經放棄了它,它可以與我自己的圖形一起工作。我現在有一個非常流暢的動畫,歡呼。 – jdubbing