4
\$\begingroup\$

In my game the main screen is the playscreen. But because I want to draw the menu and the gameover screen on top of the playscreen, I don't create separate screens for them. But if I want to replay, I must restart the playscreen. This happens very often, so should I load all the resources and dispose them when the game is closed. Because otherwise when I restart the playscreen, I have to load resources again, which will slow down the performance. Please, give me some advice.

\$\endgroup\$
0
\$\begingroup\$

If you have enough resources to fill up the lowest device you are targeting (even half way) then you need to manage your assets. If you have just a couple of MB you will be fine with leaving that in memory and only dispose when you exit your game.

But you don't need to dispose your assets between screens. If you use Assetmanager you can finely control what to dispose and what to keep even between screens.

\$\endgroup\$
0
\$\begingroup\$

Here is how I handle the assets without reloading them for each screens. You should load your asset in your main game class like this

public class MyGame extends Game {
    private AssetManager assetManager;

    @Override public void create() {
        this.assetManager = new AssetManager();

        this.assetManager.load("level.tmx", TiledMap.class);
        this.assetManager.load(...)
        this.assetManager.load(...)

        // force the asset manager to finish loading
        this.assetManager.finishLoading()

        // inject the asset manager to your screen
        this.setScreen(new LevelScreen(this.assetManager));
    }

    @Override
    public void dispose() {
        this.assetManager.dispose();
    }
}

This way if I need to reload the LevelScreen I just need to re inject the assetManager to the new screen without reloading any ressources.

To retrieve a resource from the asset manager, just use

// this retrieve the tiled map from the asset manager without reloading it
TiledMap levelMap = assetManager.get("level.tmx");
\$\endgroup\$
0
\$\begingroup\$

If you're gonna reuse the same assets there is no point in disposing them before every restart. Just reset the states of whatever you have and leave the assets alone.

You should manage your assets if you have many of them that can eat up memory, otherwise you can get away with loading everything upfront (although i believe is always good practice to manage your assets).

If you have a few assets and you will use them in every state of the game, then just load them up all together and forget about it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.