我正在使用libgdx,我试图弄清楚如何显示玩家的最终得分以及当游戏进入银幕游戏时他们的游戏的总体高分。我不确定如何将分数整数从游戏屏幕带到游戏屏幕上。
我认为您希望能够关闭游戏(关闭窗口或关闭应用程序)并为下次有人玩游戏(执行游戏或打开应用程序)存储高分。
在这种情况下,可以使用首选项,这是一个简单的示例:
游戏类
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
引用值放在变量上,以最大程度地减少错误。
[也许您不需要在游戏关闭时存储高分,所以将高分信息从一个屏幕传递到另一个屏幕应该更容易,这是一个示例:
游戏类
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);
}
}
这可能不是最好的方法,但是您可以将分数保留在扩展Game
的主类中。
import com.badlogic.gdx.Game;
public class MyGame extends Game {
private int score;
// Add getters and setters
}
然后您可以在屏幕课程中进行:
MyGame myGame = (MyGame) Gdx.app.getApplicationListener();
// Then you can set
myGame.setScore(score);
// or get the score
int score = myGame.getScore();
如果要存储多个值,则可以创建一个类并将其实例保留在MyGame
类中。您将把score
和其他属性放在此类中。
您还可以在MyGame
类中创建一个为您执行强制转换的静态方法:
public static MyGame get() {
return (MyGame) Gdx.app.getApplicationListener();
}
然后您可以执行MyGame.get().setScore(score);
或:int score = MyGame.get().getScore();