Search code examples
gradlelibgdxtexturesspritegame-physics

libgdx fails to load textures after some time


So I'm making a zombie shooter game that works well, but after running for a while I get hit with this exception:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: 
    Couldn't load file: 1zom3.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.lastride.game.Entity.changeState(Entity.java:51)
at com.lastride.game.Enemy.seek(Enemy.java:39)
at com.lastride.game.LastRideGame.update(LastRideGame.java:149)
at com.lastride.game.LastRideGame.render(LastRideGame.java:229)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.io.IOException: Error loading pixmap: 
at com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.<init>(Gdx2DPixmap.java:57)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:138)
... 9 more

I know for a fact that the image is there as it works, but it seems that after running for a bit it crashes.

This is the method where it crashes on:

public void changeState(int i)
{
    //changes the state and concurrently the image associated with the given state
    state = i;
    sprite = new Sprite(new Texture(Gdx.files.internal(name+i+suff)));//crashes here
    bounding = (sprite.getBoundingRectangle());
    if (sprite.getTexture().getTextureData().isPrepared()==false)
    {
        sprite.getTexture().getTextureData().prepare();
    }
    playerMap = sprite.getTexture().getTextureData().consumePixmap();
}

Solution

  • As @Tenfour04 points out, it could be an out-of-memory problem.

    Instead of doing this:

    sprite = new Sprite(new Texture(Gdx.files.internal(name+i+suff)));
    

    Create an global (and/or possible static and/or possible final) Texture object, and load it just once at the beginning of your game and use it repeatably.

    Something like this:

    public static Texture myTexture_1;           //<< your texture
    public static final int ID_MY_TEXTURE_1 = 1; //<< this will work as an ID
    
    // This method is called once in your game (like in the create() method)...
    public static void load() {
        myTexture_1 = new Texture(Gdx.files.internal(name + ID_MY_TEXTURE_1 + suff));
    }
    
    // when no longer necessary, remove it...
    public static void dispose() {
        myTexture_1.dispose();
        // and so on...
    }
    

    then, in your changeState() method, do a switch to retrieve the correct texture:

    public void changeState(int i) {
        state = i;
        switch(i){
            case ID_MY_TEXTURE_1 :
            sprite = new Sprite(myTexture_1);
            break;
            // rest of cases...
        }
        // rest of your code...
    }