我正在使用libgdx库开发游戏.
当我第一次加载程序时,纹理加载完美,一切都很好
当我关闭应用程序,并再次加载它(我假设Android以某种方式从内存缓存它) - 错误的纹理被加载.
如果我从历史中清除游戏,然后再试一次,它就能完美运行.
- 它目前的工作方式如下 - 我使用a SpriteBatch
绘制实际游戏.我有单独的SpriteBatche
s来绘制背景和界面(加载很好).在处理一个级别时,我处置了SpriteBatch
.
for (Block block : world.getDrawableBlocks(this.width, this.height)) { spriteBatch.draw(block.getTexture(1f), block.getPosition().x, block.getPosition().y, block.SIZE_X, block.SIZE_Y); }
-
我使用缓存加载的纹理我自己编写,以防止同一图像被加载多次.我在创建应用程序时清除缓存.然后我在对象本身保留一个Texture/TextureRegion,它是通过获得的.getTexture()
这是我用来加载纹理的代码
public static Texture loadTexture(String path) { //Do we have the texture cached? if (textures.containsKey(path)) { //return it return textures.get(path); } else { //load it from the filesystem Texture texture = new Texture(Gdx.files.internal(path)); //cache it textures.put(path, texture); //return it return texture; } }
我附加了一个调试器,正在加载的纹理DO具有正确的路径.
在图片示例中,交换的纹理碰巧是字体的一部分,这是我的缓存中存储的任何内容.
-
所以,我宁愿被困在这里.
现在我正在使用在处置时手动终止进程的顽皮解决方案:
@Override public void onDestroy() { super.onDestroy(); this.finish(); android.os.Process.killProcess( android.os.Process.myPid() ); }
这有效但很脏.
当进程因异常而失败时,不会发生错误.
我猜不知道库在某种程度上缓存了自己的纹理,这些纹理在某种程度上被破坏了,但我不知道如何检查,也不知道如何清除它们.
那么,有什么想法吗?