Progress bar while loading Textures in AndEngine

丶灬走出姿态 提交于 2020-01-15 16:00:50

问题


when I start my game black screen comes for a while because resources are being loaded. I went thorough a tutorial which show how to show progress bar while leading resources I followed it and now I can see progress bar. But the problem is this when progress bar is visible every thing else is stopped. And nothing happens. Only a black screen and a progress bar on it. Can any one tell me why every thing is paused and why loadresources and loadscene methods are not working? Please provide a solution.


回答1:


You need to load your resources in a worker thread. A nice utility for doing this is AsyncTask. The guide topic Processes and Threads has an explanation of why you need something like this, as well as sample code showing how to do a simple AsyncTask that might be just what you need.




回答2:


From Engine.java:

public void onDrawFrame(final GLState pGLState) throws InterruptedException {
        final EngineLock engineLock = this.mEngineLock;

        engineLock.lock();
        try {
            engineLock.waitUntilCanDraw();

            this.mVertexBufferObjectManager.updateVertexBufferObjects(pGLState);
            this.mTextureManager.updateTextures(pGLState);
            this.mFontManager.updateFonts(pGLState);

            this.onUpdateDrawHandlers(pGLState, this.mCamera);
            this.onDrawScene(pGLState, this.mCamera);

            engineLock.notifyCanUpdate();
        } finally {
            engineLock.unlock();
        }
    }

And that's why the engine hangs and the UI gets stuck. It's possible to display a loading screen while the textures are being loaded into hardware, without freezing, let's say, a ProgressBar. It's not easy and requires a lot of code, but it's possible and doesn't require crazy hacks, just some logic.

You need to have a Resources Manager (RM) and a Scene Manager (SM) who work together (with AsyncTasks) and are responsible for loading the textures for the current scene. Since you've a BaseGameActivity you can use this Activity instance to show a fullscreen Dialog with the progress bar. The logic is:

  1. SM is asked to show Scene A
  2. SM shows the loading Dialog
  3. SM asynchronously tells the RM to load all Scene A resources into hardware (for each texture for Scene A, texture.load)
  4. RM "onSceneTexturesLoadComplete" tells SM that all textures are loaded

Since texture.load doesn't guarantee the texture is actually loaded you'll need to have a TryToDismissDialog that extends TimerTask. This TryToDismissDialog from time to time will query the Scene A textures and check if they are actually loaded:

if (texturePack.getTexture().isLoadedToHardware()) {
        texturesLoaded++;
}

If all textures are loaded you dismiss the Dialog and voilá you'll see the Scene ready.
Hope it helps


ps: this actually involves some lines of code, I've just posted here a quick steps/guide/pseudo-code. I'll not post the final solution because it's quite heavy and "project related".



来源:https://stackoverflow.com/questions/8937303/progress-bar-while-loading-textures-in-andengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!