Skip to content

Loadingscreen

Gaganx0 edited this page Oct 17, 2023 · 2 revisions

LoadingScreen Class

The LoadingScreen class is responsible for displaying a loading animation and transitioning to the main game screen from the TurretSelectSCreen once all the required assets are loaded.

Class Description

The LoadingScreen class serves as an intermediary screen between TurretSelect and transitioning to the main game. During this phase, it loads essential assets and displays a loading animation, ensuring that the player does not enter the game prematurely.

Key Functions and Methods

  1. LoadingScreen(GdxGame game)

    • Description: The class constructor for LoadingScreen.
    • Parameters: game - The game instance to which this screen belongs.
    • Initialization:
      • Initializes the SpriteBatch.
      • Loads the background image and creates a Texture object.
      • Initializes the stage for UI elements.
      • Initializes a Label element to display "Loading."
      • Sets the UI skin using the uiskin.json file.
  2. render(float delta)

    • Description: Called in the game loop to render the loading screen and handle the transition to the main game.
    • Parameters: delta - The time in seconds since the last frame.
    • Processing Steps:
      • Renders the background image and loading animation.
      • Checks if all assets are loaded or if a set time has elapsed (1.5 seconds).
      • If the loading is complete, triggers the transition to the main game.
      • Updates the loading label with dots to indicate the loading progress.
  3. resize(int width, int height)

    • Description: Called when the screen should resize itself, maintaining the aspect ratio.
    • Parameters: width - The new width of the screen, height - The new height of the screen.
  4. pause()

    • Description: Called when the application is paused. No specific handling is implemented in this class.
  5. resume()

    • Description: Called when the application is resumed from a paused state. No specific handling is implemented in this class.
  6. hide()

    • Description: Called when this screen is no longer the current screen for the game. No specific handling is implemented in this class.
  7. dispose()

    • Description: Called when disposing of the screen to free up resources.
    • Resource Cleanup:
      • Disposes of the SpriteBatch, background texture, and any other resources created in the constructor.

How It Connects to Other Classes

  • LoadingScreen is set using game.setScreen(GdxGame.ScreenType.LOAD_SCREEN) in the turret select.
  • It transitions to the main game screen using the game.setScreen(GdxGame.ScreenType.MAIN_GAME) method when the loading is complete.
  • The loading progress is tracked using AssetLoader.areAllAssetsLoaded() to ensure that all game assets are loaded before entering the main game. The assets being loaded are managed by other classes within the game.

Code Sample

Here is a code snippet representing the key aspects of the LoadingScreen class:

public class LoadingScreen implements Screen {
   if (AssetLoader.areAllAssetsLoaded() || elapsedTime >= 1.5) {
            transitionToMainGame = true;
        }

        if (transitionToMainGame) {
            // Transition to the main game screen
            game.setScreen(GdxGame.ScreenType.MAIN_GAME);
        }
        dotTimer += delta;
        int numDots = (int) (dotTimer % 1 * 3); // Add a dot every second

        // Add dots to the label text
        StringBuilder labelText = new StringBuilder("Loading");
        for (int i = 0; i < numDots; i++) {
            labelText.append('.');
        }
        loadingLabel.setText(labelText);

In the testing phase, we initially attempted to simulate various scenarios through general mocking by creating a test file. However, it became evident that this approach was not feasible. Consequently, we shifted to a more visual approach. This testing method involved a series of actions, such as adjusting the screen resolution from the settings menu, and repeatedly minimizing and maximizing the screen. The primary goal was to verify the consistency of the sprites with both the background image and the background image in relation to changes in screen size.

Clone this wiki locally