gradle - libgdx一段时间后无法加载纹理

标签 gradle libgdx textures sprite game-physics

因此,我正在制作一款效果不错的僵尸射击游戏,但是在运行了一段时间后,却遇到了以下异常情况:

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

我知道一个事实,就是它可以正常工作,但是运行了一段时间后似乎崩溃了。

这是它崩溃的方法:

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

最佳答案

正如@ Tenfour04指出的那样,这可能是内存不足的问题。

而不是这样做:

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

创建一个全局(和/或可能的static和/或可能的final)Texture对象,并在游戏开始时将其加载一次,然后重复使用。

像这样:

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...
}

然后,在changeState()方法中,进行切换以检索正确的纹理:

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...
}

关于gradle - libgdx一段时间后无法加载纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811281/

相关文章:

gradle - Gradle变量范围

java - NoOptions 无法解析为类型

opengl - texelFetch() 中的 samplerBuffer

java - 错误 :Execution failed for task ':app:dexDebug'

java - 在 Gradle 中获取 POM 父级

android - 您可以从命令行检查项目的 Android Gradle 插件版本吗?

ios - 在 Robovm 绑定(bind)中将 AdColonyAdDelegate 作为参数传递时,AdColony 视频广告崩溃

java - 自定义着色器导致 Android 黑屏

iphone - 将 480 x 320 背景渲染为 iPhone OpenGL ES 纹理的最快方法

c++ - 没有 OpenGL 的纹理映射