简体   繁体   English

如何保存游戏的state

[英]How to save state of game

I am trying to get my app to save when closed down or if back button is pressed.我试图让我的应用程序在关闭或按下后退按钮时保存。 I'd like for it to remember which boxes are checked and what the score is on.我想让它记住选中了哪些框以及得分是多少。

I've tried looking through previous articles to save the checkbox state to shared preferences but it doesn't seem to do anything.我尝试查看以前的文章以将复选框 state 保存到共享首选项,但它似乎没有任何作用。 I've tried at least 5 different ways that I've found on here but it either just crashes the app or does nothing at all.我已经尝试了至少 5 种我在这里找到的不同方法,但它要么只是让应用程序崩溃,要么什么都不做。

This is the code I've got:这是我得到的代码:

public class levelOneActivity extends AppCompatActivity {

    TextView scoreTextView;
    Button backButton;
    ConstraintLayout pictureConstraint;
    Button nextLevelButton;
    ListView levelOneListView;
    ArrayAdapter arrayAdapter;
    int score;
    public static final String PREFS_NAME = "MyPrefsFile";
   // SharedPreferences sharedPrefs = context.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);



    public void backButton(View view) {
        pictureConstraint.setVisibility(View.INVISIBLE);
    }

    public void nextLevelButton(View view) {
        Log.i("info", "next level!");
        Intent intent = new Intent(getApplicationContext(), levelTwoActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_one);
        levelOneListView = findViewById(R.id.levelOneListView);
        scoreTextView = findViewById(R.id.scoreTextView);
        pictureConstraint = findViewById(R.id.pictureConstraint);
        backButton = findViewById(R.id.backButton);
        final ImageView imageView2 = findViewById(R.id.imageView2);
        final TextView factTextView = findViewById(R.id.factTextView);
        score = 0;
        final ConstraintLayout levelFinishedConstraint = findViewById(R.id.finishedLevelConstraint);
        nextLevelButton = findViewById(R.id.nextLevelButton);

        final SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.woodlandwanderer", Context.MODE_PRIVATE);


        final String[] levelOneListList = new String[]{
                "Daisy", "Rock", "Tree", "Dandelion", "Grass"
        };

        final int[] imageList = new int[]{
                R.drawable.daisy, R.drawable.rock, R.drawable.tree, R.drawable.dandelion, R.drawable.grass
        };

        final String[] factList = new String[]{
                "Daisies are cool", "Rocks are fun to throw!", "Trees have leaves!", "Dandelions are yellow", "Grass is green"
        };


        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, levelOneListList);


        levelOneListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                pictureConstraint.setVisibility(View.VISIBLE);
                imageView2.setImageResource(imageList[i]);
                factTextView.setText(factList[i]);
                return true;
            }
        });

        levelOneListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        levelOneListView.setAdapter(arrayAdapter);


        levelOneListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.i("Info", "Clicked");
                CheckedTextView checkedTextView = (CheckedTextView) view;


                if (checkedTextView.isChecked()) {

                    if (score < 9) {
                        score++;
                    } else {
                        score++;
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                levelFinishedConstraint.setVisibility(View.VISIBLE);
                            }

                        }, 1000);
                    }
                } else {
                    Log.i("Info", "Not checked");
                    score = score - 1;
                }

                scoreTextView.setText(Integer.toString(score) + "/10");
            }
        });
    }
}

I would suggest you to come up with an xml-file in which you set your own tag for check or unchecked boxes.我建议您提出一个 xml 文件,您可以在其中为选中或未选中的框设置自己的标签。 Whenever the user exits the programm or presses the back button, the last thing before the application shut down is saving the current state of the checked boxes and replace the old version.每当用户退出程序或按下后退按钮时,应用程序关闭之前的最后一件事就是保存当前选中的 state 并替换旧版本。

Another way to save the current state would be the database.保存当前 state 的另一种方法是数据库。 I would modify it so can even type in xml-style, or you just push the names of the checked boxes to your database.我会对其进行修改,以便甚至可以输入 xml 样式,或者您只需将选中框的名称推送到您的数据库中。 Always the last step before shutting the application down.始终是关闭应用程序之前的最后一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM