Skip to content

SpaceMiniTransition‐Exit button

Shaivika Anand edited this page Sep 14, 2023 · 1 revision

SpaceMiniTransition Class Documentation

Introduction

  • The SpaceMiniTransition class is a fundamental component in LibGDX game development, designed to facilitate seamless screen transitions with visually appealing effects. It empowers game developers to smoothly switch between different game screens, providing a visually engaging experience for players.

Table of Contents

  1. Overview
  2. Constructor
  3. Methods
  4. Properties
  5. Example Usage
  6. Cleanup

Overview

The SpaceMiniTransition class extends the LibGDX Actor class, making it easy to incorporate into the UI of a LibGDX game. It employs a clever technique of moving a transition image diagonally across the screen to create a transition effect. Once the transition is complete, it can trigger a callback or facilitate the transition to a new game screen.

Constructor

SpaceMiniTransition(GdxGame game, String alert, Skin skin, String alertText)

Creates a new SpaceMiniTransition instance.

  • game: The main game instance.
  • alert: A string representing the type of alert (e.g., "Exit game").
  • skin: The skin used for UI elements (not used in this class).
  • alertText: The text for the alert (not used in this class).

Methods

act(float delta)

Updates the actor's state on each frame.

  • delta: The time elapsed since the last frame in seconds.
  @Override
    public void act(float delta) {
        if (isTransitioning) {
            // Move the transition image diagonally
            transitionX += transitionSpeedX * delta;
            transitionY += transitionSpeedY * delta;
            if (transitionX >= Gdx.graphics.getWidth() && transitionY >= Gdx.graphics.getHeight()) {
                // Transition is complete
                transitionX = Gdx.graphics.getWidth();
                transitionY = Gdx.graphics.getHeight();
                isTransitioning = false;
                // Change the screen after the transition
                // Replace with your actual screen class
                if (isExit)
                    this.callback.run();
                else
                    game.setScreen(GdxGame.ScreenType.MAIN_MENU);
            }
        }
    }

draw(Batch batch, float parentAlpha)

Draws the actor on the screen.

  • batch: The batch used for rendering.
  • parentAlpha: The alpha value from the parent actor (not used in this class).
   @Override
    public void draw(Batch batch, float parentAlpha) {
        if (isTransitioning) {
            // Draw the transition image at the current X and Y positions
            batch.draw(transitionSprite, transitionX, transitionY);
        }
    }

showDialog(Stage stage, Runnable callback)

Displays the SpaceMiniTransition actor on the specified stage and sets a callback to execute after the transition completes.

  • stage: The Stage where the actor will be displayed.
  • callback: The callback to execute after the transition completes.
 public void showDialog(Stage stage, Runnable callback) {
        this.callback = callback;
        stage.addActor(this);
    }

dispose()

 public void dispose() {
        // Dispose of resources when done
        transitionImage.dispose();
        spriteBatch.dispose();
    }

Disposes of resources used by the SpaceMiniTransition instance. Call this method when the transition effect is no longer needed.

Properties

  • private GdxGame game: The main game instance.
  • private Runnable callback: The callback to execute after the transition.
  • private boolean isTransitioning: Indicates whether the transition is in progress.
  • private Texture transitionImage: The transition image.
  • private SpriteBatch spriteBatch: The SpriteBatch for rendering the transition image.
  • private Sprite transitionSprite: The transition sprite for rendering.
  • private float transitionX: X-axis position for the transition.
  • private float transitionY: Y-axis position for the transition.
  • private float transitionSpeedX: Speed of the X-axis transition.
  • private float transitionSpeedY: Speed of the Y-axis transition.
  • private boolean isExit: Indicates if the transition is an exit transition.

Example Usage

// Create a SpaceMiniTransition instance
SpaceMiniTransition transition = new SpaceMiniTransition(game, "Exit game", skin, "Exit confirmation"); ```

// Show the transition on a stage with a callback
transition.showDialog(stage, () -> {
    // Callback code here
});

// Update and render the transition in your game loop
transition.act(delta);
transition.draw(batch, parentAlpha);

## Cleanup

Call the dispose() method when you no longer need the SpaceMiniTransition instance to release any allocated resources.

```transition.dispose();```
Clone this wiki locally