如何在Android中保存游戏中的高分?
我正在用Android做一个小游戏,但我不知道用SharedReferences来保存高分。当我在GamePlayActivity中设定点时,我想用新的highscore替换旧的highscore,如果它大于旧的highscore 这是我的密码:如何在Android中保存游戏中的高分?,android,sharedpreferences,Android,Sharedpreferences,我正在用Android做一个小游戏,但我不知道用SharedReferences来保存高分。当我在GamePlayActivity中设定点时,我想用新的highscore替换旧的highscore,如果它大于旧的highscore 这是我的密码: import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPrefe
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>
在SharedReference中存储数据:
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);
从共享首选项获取数据:
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));
}
我们将声明一个静态变量,如
公共静态int score、HighScore、prevatemptscore
每次游戏结束时检查,根据您从SharedPreferences获得的先前高分更新分数
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();
}
}
更改后,最好将您的游戏高分保存在SharedReferences中。在onDestroy()期间不要保存它,因为如果用户在屏幕上玩游戏时,设备的电池已耗尽,该怎么办。。。?它可能不会立即保存 用以下代码替换整个代码:
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();
}
});
}
}
当您在if语句中比较highScoreCurrent和highScore时,两者都尚未初始化,因此它们的默认值为0。还有int分数和int highScore之间的区别是什么?Nice的可能重复的可能重复!但我在MainActivity中有一个按钮(高分),当我构建时,为什么这个游戏过度活跃会首先出现?我不太理解你,但我理解的是,在你的MainActivity中有一个高分按钮。现在怎么办?您可以在HighScoreActivity的onCreate()中正常加载您的高分,就像在GameOverActivity中加载高分一样。等等,您的意思是当您单击高分按钮时GameOverActivity被打开?你可以查看它的onClickListener,看看它打开的目的是什么。在我的应用程序GameOverActivity中(这意味着HighScoreActivity)。这个问题只有在我在android studio上构建这个应用程序时才会发生,当它安装在genymotion上时,就不会再发生了。到底是什么问题?请进一步澄清。