android - 如何在游戏结束屏幕上显示最终分数和高分?
问题描述
我正在使用 libgdx,我试图弄清楚如何显示我的玩家达到的最终分数,以及当游戏进入游戏画面时他们游戏的总体高分。我不确定如何将分数整数从游戏屏幕带到游戏结束屏幕。
解决方案
选项 1:使用首选项存储高分
我认为您希望能够关闭游戏(关闭窗口或终止应用程序)并为下次有人玩游戏(执行游戏或打开应用程序)存储高分。
在这种情况下,偏好是要走的路,这是一个简单的例子:
游戏课
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));
}
}
游戏画面类
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));
}
}
游戏结束屏幕类
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:在屏幕之间传递高分
也许您不需要在游戏关闭时存储高分,因此将高分信息从一个屏幕传递到另一个屏幕应该更容易,这里有一个示例:
游戏课
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));
}
}
游戏画面类
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));
}
}
游戏结束屏幕类
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);
}
}