0
\$\begingroup\$

For my game, Im using

  • libGDX
  • Ashley (ECS)
  • Box2D

Ive got a lot of screens but for simplification: MainMenuScreen and InGameScreen.

In InGameScreen Im init the ECS (Ashley) and create a lot of entities. Some have PhysicsComponent. The PhysicsSystem will catch them and call Box2D to spawn some objects based on the parameters within the PhysicsComponents.

So far so good.

Now, when the player dyes, I run setScreen(new MainMenuScreen()); from my "Main"-Class (MyGame.java).

  1. the game starts and MyGame loading at first the MainMenuScreen
  2. than the InGameScreen will get loaded (when player presses a key)
  3. than the player dyes
  4. MyGame recognized it and loading again (throu calling setScreen(...)) the MainMenuScreen
  5. When Player press key: MyGame loading another round (setScreen(new InGameScreen))

I guess I have to do something like a complete clean up between switching screens. But how should I implement this? Just "deleting" the old InGameScreen-Object shouldent be enought, right?

  • Do I have to use the dispose()-Method?
  • Is it enough to instantiate everything new (in the init from InGameScreen) since it will instantiate all child-object new for its own like Box2DWorld world = new World();
\$\endgroup\$
1
\$\begingroup\$

Making a new instance of a screen, each time you set it is ok. It will clean everything in your screen.

Since InGameScreen will need to reset every time you set it, it's a possibility.

But for screen that don't need to be reloaded / cleaned like a menu screen, you could use singletons. This way you only share one instance of the screen in your code and don't reload it for nothing.

Basically:

  • Screen which need clean reload - setScreen(new YourScreen);
  • Screen which don't need clean reload (which contain a simple menu with user interactions) - setScreen(YourMenuScreen.instance);

You can push this arthictecture by making all screens singletons, set up the screen (load what need to be loaded) by overriding Screen.show() (called on setScreen) and clean everything that need to be cleared by overriding Screen.hide() (called when screen is not longer active screen).

This way, all your screens are instanciated at start. Your screen load the strict necessary on show, and clear (clear collection for example, instead of re-instanciating by instanciating the screen each time you set it) what's need to be cleared when it's no longer the active screen). You will win a certain amount of time at runtime with this architecture ;)

Hope it helps !

\$\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.