0

I'm trying to produce a simple 18 frame animation using Texturepacker with libGDX (other info: Eclipse, Windows 8).

I'm getting the following LogCat error, coupled with a black screen on my Android test device:

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)

The LogCat error seems to point to the render section of my game class:

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

My Texturepacker png image and text document refer to each test frame as test1, test2, test3 etc.

I was previously getting errors that pointed to the line associated with OpenGL, and separately, with the line associated with camera settings. I seemed to have solved them, but perhaps this is related?

Thanks.

jdubbing
  • 99
  • 7
  • 1
    The error points to line 71 and explains you have a null pointer. Should be fairly easy to figure out, but if you need additional help you could consider sharing which line is actually 71... Wild guess; test is null or test.get(..) returns null. – Marius Kjeldahl Feb 20 '14 at 03:22
  • Hi Marius, line 71 points to batch.begin(); – jdubbing Feb 20 '14 at 03:30

1 Answers1

1

You havnt initialize batch. insert
batch = new SpriteBatch();
in your create() method.
Apart form this , your code
float dt = Gdx.graphics.getDeltaTime(); animationElapsed += dt; while(animationElapsed > frameLength){ animationElapsed -= frameLength; currentFrame = (currentFrame == test.size - 1) ? 0 : ++currentFrame; }
should be placed inside render() .

Pranav008
  • 1,225
  • 8
  • 11
  • Thanks Pranav- I'm no longer getting any errors in my LogCat. I still don't see the animation on screen though (separate problem). I'm guessing it might be to do with my camera settings, but I can't seem to fix it. – jdubbing Feb 20 '14 at 04:00
  • 1
    use batch.setProjectionMatrix(camera.combined); in your render(). Secondly you havnt initialize any value to frameLength. Thirdly why are you not drawing animation using Animation class object provided in libgdx. In case you are new in libgdx and dont have info about animation class then take a look at this http://code.google.com/p/libgdx/wiki/SpriteAnimation – Pranav008 Feb 20 '14 at 04:45
  • Thanks Pranav. I discovered the tutorial you mentioned in the link about an hour ago (much easier to follow than the other tutorial I was using). I've given it a go, and it works with my own graphics. I have a really smooth animation now, cheers. – jdubbing Feb 20 '14 at 06:04