【问题标题】:AndEngine: Load textures dynamically during gameAndEngine:在游戏过程中动态加载纹理
【发布时间】:2013-09-20 15:52:21
【问题描述】:

在我的游戏中,我需要在游戏过程中加载纹理(在角色自定义屏幕中,用于加载不同的皮肤)。但是当我这样做时,用户界面被阻止了。这是代码:

private static void loadSkinTextureRegions(){
    sTexture.clearTextureAtlasSources();
    sTextureRegionA = BitmapTextureAtlasTextureRegionFactory.createFromAsset(sTexture, context, getFileNameA(), 0, 0); 
    sTextureRegionB = BitmapTextureAtlasTextureRegionFactory.createFromAsset(sTexture, context, getFileNameB(), 0, 406); 
}

所以我想:“我可以使用 AsyncTask 运行这段代码”。于是我试了一下,现在UI没有被阻塞但是……有时候贴图加载不出来,不知道为什么!?

有什么方法可以在不阻塞线程的情况下动态加载纹理?

编辑:使用 AsyncTask 进行纹理更改,有时我会收到此错误:

09-20 09:43:59.218: E/AndroidRuntime(21976): FATAL EXCEPTION: GLThread 1622
09-20 09:43:59.218: E/AndroidRuntime(21976): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
09-20 09:43:59.218: E/AndroidRuntime(21976):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at java.util.ArrayList.get(ArrayList.java:308)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.writeTextureToHardware(BitmapTextureAtlas.java:152)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at org.andengine.opengl.texture.Texture.loadToHardware(Texture.java:137)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at org.andengine.opengl.texture.Texture.reloadToHardware(Texture.java:162)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at org.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:239)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at org.andengine.engine.Engine.onDrawFrame(Engine.java:613)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at org.andengine.opengl.view.EngineRenderer.onDrawFrame(EngineRenderer.java:105)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1525)
09-20 09:43:59.218: E/AndroidRuntime(21976):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1242)

如果我同步执行,效果很好,但它会阻塞线程并且很烦人

【问题讨论】:

  • 你如何确定没有加载纹理?当加载似乎失败时,您是否注意到一种模式?
  • 纹理没有加载,因为精灵消失了。我没有注意到任何模式。我放了一个更改纹理的按钮,我触摸它几次,所以每次触摸它都会改变纹理,有时精灵会消失,如果我再次触摸按钮,精灵就会再次可见:(
  • 你能发布整个 Logcat 吗?这并不指向您的任何代码。
  • 代码是我贴的代码。我看到这失败了,因为我从另一个线程中删除并添加了纹理源,而 andengine 正在迭代它

标签: android andengine


【解决方案1】:

尝试在每次创建纹理区域后加载纹理。 在你的情况下

       private static void loadSkinTextureRegions(){
        sTexture.clearTextureAtlasSources();
        sTextureRegionA = BitmapTextureAtlasTextureRegionFactory.createFromAsset(sTexture, context, getFileNameA(), 0, 0); 
        sTextureRegionB = BitmapTextureAtlasTextureRegionFactory.createFromAsset(sTexture, context, getFileNameB(), 0, 406); 
 sTexture.load();

    }

您也可以将 loadSkinTextureRegions() 放在 UIThread 中。

【讨论】:

  • 谢谢,但它是一样的。有时精灵会消失(当我进行纹理更改时):(
猜你喜欢
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多