【问题标题】:Saving a counter in libGDX在 libGDX 中保存计数器
【发布时间】:2014-06-26 10:03:13
【问题描述】:

我正在为 android 开发一款 libgx 游戏。 我在屏幕上有一个计数器,当我触摸某些东西时它会增加。 在那场比赛中你可以死,但我想保存玩家的最高分。 我不知道从哪里开始。我是android游戏开发的新手。 这是你死时的方法。

if (player.getBounds().overlaps(rock.getBounds()){
        System.out.println("Game Over");
    }

这里我增加了计数器

        if (player.getBounds().overlaps(food.getBounds()) {
            System.out.println("Food");
            counter++;
                        }

谢谢

【问题讨论】:

  • 你不能把它写在一个文件里吗?
  • 先生,我真的不太了解 libgdx。如果您能帮我解决这个问题,我将不胜感激。
  • @user2959870 查看File module。可悲的是,没有编写文件的教程... 自己找到如何做到这一点应该不会太难,看FileHandlejavadoc
  • 是的。阅读关于创建的高分。如果玩家获得高分,请将其写入文件。应该很简单。

标签: android libgdx screen overlap bounds


【解决方案1】:

听起来您需要的是首选项。它们提供了一种独立于平台的方式来保存少量数据。它们是高分之类的理想之选。

调用 loadPrefs() 从存储中加载并调用 savePrefs() 将它们保存回来。

public static Preferences prefs;  
public static int counter;    // The variable you want to save

//Save the shared preferences
public static void savePrefs(){
    prefs = Gdx.app.getPreferences("game-prefs");  // The name of your prefs files
    prefs.putInteger("counter", counter);    //counter is your counter you wish to save. "counter" is the name of the varable saved in shared preferences


    prefs.flush();  //Save preferences
}

//Load the shared preferences

public static void loadPrefs(){

    prefs = Gdx.app.getPreferences("game-prefs");
    counter = prefs.getInteger("counter",0)  //Load counter, default to zero if not found

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2023-03-30
    • 1970-01-01
    • 2016-09-11
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多