如何在Android中保存游戏中的高分?

时间:2015-09-24 15:20:48

标签: android sharedpreferences

我正在Android中制作一款小游戏,但我不知道如何通过使用SharedPreferences来保存高分。当我在GamePlayActivity中意图点时,如果它比旧的更高,我想用更新的高分替换旧的高分。

这是我的代码:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GameOverActivity extends Activity{

    TextView tvHighScore, tvScore;
    Button btnRetry;
    int score = 0;
    int highScore = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_gameoveractivity);



        tvHighScore = (TextView) findViewById(R.id.tvHighScore);
        tvScore = (TextView) findViewById(R.id.tvScore);
        btnRetry = (Button) findViewById(R.id.btnRetry);

        Intent getIntent = getIntent();
        score = getIntent.getIntExtra("point", 0);
        tvScore.setText(Integer.toString(score));

        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        highScore = prefs.getInt("score", 0);

        if(score > highScore){
            highScore = score;
            tvHighScore.setText(Integer.toString(highScore));
        }

        btnRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GameOverActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("score", highScore);
        editor.commit();
        super.onDestroy();
    }
}

和XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/uefa_champion_league_background">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="GUESS THE \n FOOTBALL PLAYER"
        android:textSize="30dp"
        android:typeface="serif"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:textColor="@color/red"
        android:textStyle="bold"
        android:shadowColor="@color/green2"
        android:shadowDx="5"
        android:shadowDy="5"
        android:shadowRadius="15"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="186dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@color/blue25"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="GAME OVER"
            android:textSize="40dp"
            android:typeface="serif"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_marginTop="5dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="20dp">

            <LinearLayout
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TOP SCORE"
                    android:typeface="serif"
                    android:textSize="15dp"/>

                <TextView
                    android:layout_width="100dp"
                    android:layout_height="40dp"
                    android:text="1"
                    android:textSize="30dp"
                    android:typeface="serif"
                    android:gravity="center"
                    android:textColor="@color/red2"
                    android:textStyle="bold"
                    android:id="@+id/tvHighScore"/>

            </LinearLayout>

            <Button
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:text="RETRY"
                android:textSize="30dp"
                android:gravity="center"
                android:background="@drawable/button_shape2"
                android:id="@+id/btnRetry"/>

            <LinearLayout
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp">

                <TextView
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="SCORE"
                    android:typeface="serif"
                    android:textSize="15dp"
                    android:gravity="center"/>

                <TextView
                    android:layout_width="100dp"
                    android:layout_height="40dp"
                    android:text="3"
                    android:textSize="30dp"
                    android:typeface="serif"
                    android:gravity="center"
                    android:textColor="@color/red2"
                    android:textStyle="bold"
                    android:id="@+id/tvScore"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>


</LinearLayout>

3 个答案:

答案 0 :(得分:2)

最好在SharedPreferences更改后保存游戏高分。不要在onDestroy()期间保存它,因为如果用户设备的电池在Game Over屏幕上死了怎么办?它可能不会立即保存。

用以下代码替换整个代码:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GameOverActivity extends Activity {

    TextView tvHighScore, tvScore;
    Button btnRetry;
    int score;
    int highScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_gameoveractivity);

        tvHighScore = (TextView) findViewById(R.id.tvHighScore);
        tvScore = (TextView) findViewById(R.id.tvScore);
        btnRetry = (Button) findViewById(R.id.btnRetry);

        score = getIntent().getIntExtra("point", 0);
        tvScore.setText(Integer.toString(score));

        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        highScore = prefs.getInt("score", 0);

        if (highScore > score) {
            tvHighScore.setText(Integer.toString(highScore));
        } else {
            highScore = score;
            tvHighScore.setText(Integer.toString(highScore));
            prefs.edit().putInt("score", highScore).apply();
        }

        btnRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GameOverActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

答案 1 :(得分:0)

在Sharedpreferences中存储数据:

 SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,     
  MODE_PRIVATE).edit();
  editor.putString("name", "abcd");
 editor.putInt("score", 12);
 editor.commit();

从共享首选项中获取数据:

 SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,     
 MODE_PRIVATE); 
 String text1= prefs.getString("name", "0");
 int text2= prefs.getInt("score", 0);

您可以使用if条件检查分数并将其设置在您想要的位置

像这样

    if(highScoreCurrent > highScore){
        highScore = highScoreCurrent; 
        editor.putInt("score", highScore); 
        editor.commit();
        tvHighScore.setText(Integer.toString(highScore));
    }

答案 2 :(得分:0)

请声明一个静态变量,例如public static int score,HighScore,prevattemptScore;

每次游戏结束时都要检查,根据您从SharedPrefernces获得的上一个高分更新分数。

     SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        prevHighScore= prefs.getInt("score", 0);
   if(score > prevattemptScore)//checking for everyattempt
   {
     HighScore=score;
    }
     public void onDestroy(){ //while closing the game save highscore based on latest score value 
        if(HighScore > prevHighScore)
        {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("score", HighScore);
        editor.commit();
        }
       }