1

我是新手,我知道我会因为这个问题而被打败,但如果我得到答案,那将是值得的。我有一个游戏,当用户赢得第一轮比赛时,我希望保存分数并重新启动活动,而不是遍历所有代码并重置每个值。现在如何在活动重新启动时保持分数不重置。我还没有例子。

 public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
// SET UP SPINS
private TextView spins;
private Button spin;

// SET UP CONTROLS
private TextView score;
private ProgressBar progressBar;
private static final int SHOW_BONUSACTIVITY = 1;
    int nextspin = 0;
int alert;
int total = 0;
int totalspins = 14;
public int gamescore = 0;
public int currentgamescore = 0;

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.play_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

这是我第一次检查是否有新游戏或是否保存了要在此处使用的高分的地方

if (GameState.INSTANCE.getScore() != 0){
        gamescore = GameState.INSTANCE.getScore();}

如果用户获胜,他们将被发送到奖励游戏,然后返回查看他们是否已完成游戏并需要开始新游戏

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case (SHOW_BONUSACTIVITY): {
        if (resultCode == Activity.RESULT_OK) {
            int bonus = data.getIntExtra("money", 0);
            gamescore=(gamescore + bonus);
            score.setText(String.valueOf("SCORE: " + gamescore));
            if (bigwin == true){

如果他们赢得了第一关,我将在此处保存游戏分数以将其带入新活动。

                GameState.INSTANCE.addScore(gamescore);
                Intent intent = new Intent(
                MainActivity.this,
                com.bigdaddyapp.android.blingo.MainActivity.class);
                startActivity(intent);  


            }
            }
        }
    }
    }

下一个代码是枚举活动

public enum GameState {
INSTANCE;

  private int score;
  private GameState(){
    score = 0;
  }

  public int getScore(){
    return score;
  }
  public void addScore(int score){
    this.score += score;
  }

}
4

3 回答 3

3

您可以使用SharedPreferences作为一种简单的持久存储机制来保存分数、生命等内容。当关卡结束时,通过putInt()保存分数,当您想要获得分数时使用getInt()

于 2012-06-30T16:00:07.833 回答
2

作为持久存储值的替代方法(使用共享首选项或 SQLite 数据库),您还可以将值存储在内存中,但存储在更全局的上下文中。这样,您将在重新创建 Activity 时保持状态,但应用程序在关闭时会忘记它们。

对于您的特定情况,这可能不是您所需要的,但有时它是更好的选择。


使用 Singleton,您可以更全局地存储应用程序的状态并在代码中的任何位置检索它。一个例子可能是:

public enum GameState{
  INSTANCE;

  private int score;
  private GameState(){
    score = 0;
  }

  public int getScore(){
    return score;
  }
  public void addScore(int score){
    this.score += score;
  }

}

它可以通过简单的编写来使用:

GameState.INSTANCE.addScore(20);
GameState.INSTANCE.getScore();

有多种方法可以实现单例。在我的示例中,我使用了 Joshua Bloch 在他的“Effective Java - Second Edition”一书中记录的方法。有关更多信息,请参阅维基百科


扩展android.app.Application是另一种选择,认为不像使用单例那样推荐。从JavaDoc

需要维护全局应用程序状态的基类。您可以通过在 AndroidManifest.xml 的标记中指定其名称来提供自己的实现,这将导致在创建应用程序/包的进程时为您实例化该类。

可以在此处找到有关如何执行此操作的教程(以及链接的答案)。

于 2012-06-30T16:15:20.723 回答
0

当您想结束游戏时,请执行以下操作:

  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putInt("gamevariable", gamevariable);

  editor.commit();

然后在您的 onCreate 方法中,取回您的变量并根据需要使用它:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
myvariable = settings.getInt("gamevariable", false);

作为替代方案,我会使用标准序列化,这是一个更复杂但更简单的选择。

首先将您的整数写入一个 .ser 文件,稍后您可以读取该文件并将其保存到您的项目目录中:

OutputStream os = new FileOutputStream("myinteger.ser");
ObjectOutput oo = new ObjectOutputStream(os);
oo.writeObject(myinteger);
oo.close();

然后在下次加载活动时将整数读回变量:

InputStream is = new FileInputStream("myinteger.ser");
ObjectInput oi = new ObjectInputStream(is);
int myinteger = (int) oi.readObject();
oi.close();
于 2012-06-30T16:22:20.250 回答