我如何在游戏结束屏幕上显示最终分数和高分?

How do I display a final score and high score on gameover screen?

我正在使用 libgdx,我正在尝试弄清楚如何显示我的玩家获得的最终分数,以及当游戏进入游戏结束屏幕时他们的游戏的总体高分。我不确定如何将分数整数从游戏屏幕带到游戏结束屏幕。

这可能不是最好的方法,但您可以将分数保留在扩展 Game.

的主要 class 中
import com.badlogic.gdx.Game;

public class MyGame extends Game {
    private int score;

    // Add getters and setters
}

然后在您的屏幕上 classes 您可以这样做:

MyGame myGame = (MyGame) Gdx.app.getApplicationListener();
// Then you can set
myGame.setScore(score);
// or get the score
int score = myGame.getScore();

如果您有多个值要存储,您可以创建一个 class 并将其实例保存在您的 MyGame class 中。您将把 score 和其他属性放在这个 class.

您还可以在 MyGame class 中创建一个静态方法来为您进行转换:

public static MyGame get() {
    return (MyGame) Gdx.app.getApplicationListener();
}

那你可以MyGame.get().setScore(score); 或者:int score = MyGame.get().getScore();

选项 1:使用首选项存储高分

我认为您希望能够关闭游戏(关闭 window 或终止应用程序)并存储高分以供下次有人玩游戏时使用(执行游戏或打开应用程序)。

在这种情况下,偏好是可行的方式,这是一个简单的例子:

游戏class

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class PreferenceExample extends Game {

    @Override
    public void create() {
        // Initialize the preferences
        Preferences preferences = Gdx.app.getPreferences("examplePreferences");
        // Go to your game screen sending this LibGDX Game and the LibGDX Preferences
        setScreen(new GameScreen(this, preferences));
    }
}

游戏画面class

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.ScreenAdapter;

class GameScreen extends ScreenAdapter {

    private PreferenceExample game;
    private Preferences preferences;

    GameScreen(PreferenceExample game, Preferences preferences) {
        // Store reference to LibGDX Game
        this.game = game;
        // Store reference to LibGDX Preferences
        this.preferences = preferences;
    }

    @Override
    public void render(float delta) {
        if (Gdx.input.isKeyJustPressed(Input.Keys.L)) {
            saveHighScore(MathUtils.random(3));
            goToGameOverScreen();
        }
    }

    // Call this whenever you want to save the high score
    private void saveHighScore(int highScore) {
        preferences.putInteger("High score", highScore);
        preferences.flush();
    }

    // Call this whenever you want to switch to the game over screen
    private void goToGameOverScreen() {
        game.setScreen(new GameOverScreen(preferences));
    }
}

游戏结束画面class

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.ScreenAdapter;

class GameOverScreen extends ScreenAdapter {

    private Preferences preferences;
    private int highScore;

    GameOverScreen(Preferences preferences) {
        // Store reference to LibGDX Preferences
        this.preferences = preferences;
    }

    @Override
    public void show() {
        // Load high score, default value is 0 in case you didn't store it properly
        highScore = preferences.getInteger("High score", 0);
    }

    @Override
    public void render(float delta) {
        // Do something with the high score you retrieved
        System.out.println(highScore);
    }
}

警告:请注意,从 Preferences 存储和检索方法区分大小写,因此最好将 String 引用该值在变量上以尽量减少错误。

选项 2:在屏幕之间传递高分

也许您不需要在游戏关闭时存储高分,因此将高分信息从一个屏幕传递到另一个屏幕应该更容易,这是一个示例:

游戏class

import com.badlogic.gdx.Game;

public class ScreenToScreenExample extends Game {

    @Override
    public void create() {
        // Go to your game screen sending this LibGDX Game and the LibGDX Preferences
        setScreen(new GameScreen(this));
    }
}

游戏画面class

import com.badlogic.gdx.ScreenAdapter;

class GameScreen extends ScreenAdapter {

    private ScreenToScreenExample game;
    private int highScore;

    GameScreen(ScreenToScreenExample game) {
        // Store reference to LibGDX Game
        this.game = game;
    }

    // Call this whenever you want to save the high score
    void saveHighScore(int highScore) {
        this.highScore = highScore;
    }

    // Call this whenever you want to switch to the game over screen
    void goToGameOverScreen() {
        game.setScreen(new GameOverScreen(highScore));
    }
}

游戏结束画面class

import com.badlogic.gdx.ScreenAdapter;

class GameOverScreen extends ScreenAdapter {

    private int highScore;

    GameOverScreen(int highScore) {
        this.highScore = highScore;
    }

    @Override
    public void render(float delta) {
        // Do something with the high score you retrieved
        System.out.println(highScore);
    }
}