java - Asset Manager 从内存中加载像素图 Libgdx Java

标签 java android libgdx out-of-memory

我正在创建一个 libgdx 游戏,它运行良好,完美地加载了所有 Assets 等。但是我随后尝试实现 libgdx 的 Asset Manager' 以添加 Activity 加载屏幕等。但是之后这样做我的游戏需要很长时间才能在桌面上加载,保持黑屏,几乎崩溃然后最终崩溃并且在我的 Assets 上我不断收到错误或类似的错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load 
    file: data/startBackground.png
        at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
        at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
        at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
        at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
        at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
        at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
        at com.MKgames.game1.screen.MainMenuScreen.show(MainMenuScreen.java:98)
        at com.badlogic.gdx.Game.setScreen(Game.java:61)
        at com.MKgames.Game1.render(Game1.java:41)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
    Caused by: java.io.IOException: couldn't load pixmap outofmem
        at com.badlogic.gdx.graphics.g2d.Gdx2DPixmap.<init>(Gdx2DPixmap.java:57)
        at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:138)
        ... 10 more

然后我尝试删除我的 Assets 类别,同样的事情不断发生。

data/startBackground.png 是一张 1920*1080 .png 图片。

我觉得在计算机上运行它可能比游戏本身更重要?

这是在没有 Assets 管理器的情况下正常加载 Assets 的方式(这在我尝试实现 Assets 管理器之前有效,但现在在我删除 Assets 管理器后它不再有效):

public void show() {
        Texture backgroundTexture = new Texture(Gdx.files.internal("data/startBackground.png"));
        background =  new Sprite (backgroundTexture);

}

以下是我如何使用 Assets 管理器加载此 Assets :

public class Game1 extends Game{
public void render() {
        if(Assets.update()){
            this.setScreen(new MainMenuScreen(this));
        }
    }
}

...

public class Assets {

    public static AssetManager manager = new AssetManager();

    public static void queueLoading() {
        (..)
        manager.load("data/startBackground.png",Texture.class);
        (..)
    }
    public static boolean update() {
        return manager.update();
    }
}

最佳答案

Game1 渲染方法每帧都被调用,所以你在每一帧上创建一个新的屏幕,屏幕在每次显示调用中创建一个新的纹理实例,你最终会耗尽内存。

另外,不要忘记添加

super.render();

在您的游戏类渲染方法中。否则,不会调用当前屏幕的 render()

关于java - Asset Manager 从内存中加载像素图 Libgdx Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235596/

相关文章:

java - 在 ASP.NET Web API 中创建输入参数并使用 java web 服务功能

android - android webview 版本取决于 sdk 还是设备?

java - LibGdx 物理体编辑器

android - 如何为 arm (Android) 重建核心实用程序?

android - Recycler View 中的 Intent ?

java - glGenVertexArrays() 不应有任何参数

java - LibGDX Actor 不会绘制 Action

java - 显示 Twitter 流时阻止 twitter4j 打印输出

java - 如何在 native 浏览器中打开 URI (android)

java - 如何在 android/java 中使用矩阵移动和旋转 Canvas 中的位图?