【问题标题】:AndEngine GLES 2.0 - Power button issue - Game not Resume After unlock ScreenAndEngine GLES 2.0 - 电源按钮问题 - 解锁屏幕后游戏无法恢复
【发布时间】:2013-09-17 13:41:40
【问题描述】:

我正在使用 AndEngine GLES 2.0 开发一款安卓游戏。我的一些用户面临这样的问题:锁定/解锁设备后或按下主页按钮并恢复游戏后无法完全运行意味着游戏在解锁屏幕后挂起。我的意思是这发生在调用 onResume 和 onPause 之后。

我的 onPause 代码是

@Override
protected void onPause() {
    super.onPause();
    this.mEngine.stop();
    this.getSManager().stop(Sounds.SPIN);
    dbHelper.close();
}

onResuam 代码为:

@Override
protected void onResume() 
{

 if (this.mEngine.isRunning()) 
    {
        this.getSManager().stop(Sounds.SPIN);
        this.mEngine.stop();
    }
    else 
    {
        this.mEngine.start();
    }

}

有人知道吗?请为此问题提出一些解决方案。

【问题讨论】:

    标签: android andengine


    【解决方案1】:

    尝试添加

    android:configChanges="orientation|screenLayout|keyboardHidden|screenSize"
    

    到你的主要活动。

    另外:确保您的纹理都不是静态变量。不知道为什么,但是对纹理使用静态变量会使引擎恢复运行。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,我已经通过重新加载 Activity 的 OnResume 方法中的所有纹理和字体来修复它。

      编辑回答整形外科医生:

      我有一个类TextureCache 正在缓存纹理。它工作正常,但在应用程序恢复时,所有纹理都会变成黑色。正如您所说,这似乎是静态变量的问题。我添加了一个清除静态数据的方法clear,并在应用程序恢复时调用它。

      这是我的课:

      public class TextureCache {
      private static Map<String, ITextureRegion> textures = new HashMap<String, ITextureRegion>();
      private static Context ctx;
      private static TextureManager tm;
      
      public static void clear() {
          textures.clear();
      }
      
      
      public static void init(Context pCtx, TextureManager pTm) {
          ctx = pCtx;
          tm = pTm;
      }
      
      
      public static ITextureRegion getTexture(String name) {
          ITextureRegion texture = textures.get(name);
          if (texture == null) {
              BitmapFactory.Options opt = new BitmapFactory.Options();
              opt.inJustDecodeBounds = true;
              try {
                  InputStream in = ctx.getResources().getAssets().open("gfx/" + name);
                  BitmapFactory.decodeStream(in, null, opt);
              } catch (IOException e) {
                  Log.e("TextureCache", "Could not load texture [" + name + "]", e);
              }
              BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
              BitmapTextureAtlas texAtlas = new BitmapTextureAtlas(tm, opt.outWidth, opt.outHeight, TextureOptions.DEFAULT);
              texture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texAtlas, ctx, name, 0, 0);
              texture1.load();    
              textures.put(name, texture);
          }
          return texture;
      }
      }
      

      我在Activity的onResume中调用了clear方法:

      @Override
      protected synchronized void onResume() {
          TextureCache.clear();
          super.onResume();
      }
      

      【讨论】:

      • 这看起来也是一个不错的解决方案。你介意在有时间的时候发布一些代码吗?
      • @PlasticSturgeon 完成!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多