需要保存一个高的分数一款Android游戏 [英] Need to save a high score for an Android game
问题描述
这很简单,我需要做的是保存了很高的分数(整数)的游戏。我假设这样做是将其存储在一个文本文件中最简单的方法,但我真的不知道如何去这样做。
It's quite simple, all I need to do is save a high score (an integer) for the game. I'm assuming the easiest way to do this would be to store it in a text file but I really have no idea how to go about doing this.
推荐答案
如果你需要的是存储为整数,然后的共享preferences 将是最适合您使用的:
If all you need is to store an integer then SharedPreferences would be best for you to use:
//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();
要获得preference:
To get a preference:
//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
当然,替换键
用钥匙你的高分价值和我的prefsKey
与键为您的preferences(这可能是别的什么东西。这是刚刚好将它们设置为可识别的东西和独特的)。
Of course replace "key"
with the key for your high score value and "myPrefsKey"
with the key for your preferences (These can be whatever. It's just good to set them to something recognizable and unique).
这篇关于需要保存一个高的分数一款Android游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!