在 AndEngine 中加载纹理时的进度条

发布于 2024-12-28 00:34:54 字数 187 浏览 1 评论 0原文

当我开始游戏时,黑屏会出现一段时间,因为正在加载资源。 我仔细阅读了一个教程,该教程展示了如何在引导资源的同时显示进度条,我遵循了它,现在我可以看到进度条。但问题是,当进度条可见时,其他所有事情都会停止。但什么也没发生。只有黑屏和进度条。谁能告诉我为什么每件事都暂停以及为什么 loadresources 和 loadscene 方法不起作用?请提供解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2025-01-04 00:34:54

您需要在工作线程中加载资源。执行此操作的一个不错的实用程序是 AsyncTask。指南主题 进程和线程 解释了原因您需要这样的东西,以及演示如何执行简单的 AsyncTask 的示例代码,这可能正是您所需要的。

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.

江湖彼岸 2025-01-04 00:34:54

来自 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();
        }
    }

这就是引擎挂起且 UI 卡住的原因。可以在纹理加载到硬件时显示加载屏幕,而不会冻结,比如说 ProgressBar。这并不容易,需要大量代码,但它是可能的,不需要疯狂的黑客,只需要一些逻辑。

您需要有一个资源管理器(RM)和一个场景管理器 (SM) 协同工作(与 AsyncTasks)并负责加载当前场景的纹理。由于您有一个 BaseGameActivity,您可以使用此 Activity 实例来显示带有进度条的全屏Dialog。逻辑是:

  1. SM被要求显示场景A
  2. SM显示加载Dialog
  3. SM异步告诉RM将所有场景A资源加载到硬件中(对于场景 A 的每个纹理,texture.load)
  4. RM“onSceneTexturesLoadComplete”告诉 SM 所有纹理都已加载。

由于texture.load 不能保证纹理实际加载,因此您需要有一个 TryToDismissDialog 那个扩展TimerTask。此 TryToDismissDialog 会不时查询场景 A 纹理并检查它们是否实际已加载:

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

如果所有纹理均已加载,您将关闭Dialog< /code> 瞧,你会看到场景准备好了。
希望有帮助

ps:这实际上涉及一些代码行,我刚刚在这里发布了快速步骤/指南/伪代码。我不会发布最终的解决方案,因为它非常重并且“与项目相关”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文