我正在开发一款游戏,并且我具有中级代码知识。我的游戏一共有 10 个关卡。我立即记录分数并与该级别的高分进行比较,这更多。
我想保留每个级别的高分数据,并将其与所有级别一起收集并打印 TextView。
您将在我尝试使用 Sharedpreferences 的代码中看到。结果:每个级别都将自己的高分写入 TextView。我希望将所有级别的高分总和写入 TextView。
public class Main2Activity extends AppCompatActivity {
TextView scoreText,failedText,successText,highScoreText,randomArray;
int score;
public static int highScore,totalScore,totalScore2;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
score = 0;
//totalScore = highScore + totalScore2;
//totalScore2 = key.getInt("high_score_level2",0);
//Toast.makeText(this, "Genel Skor : "+ totalScore , Toast.LENGTH_LONG).show();
Toast.makeText(this, "High : "+ highScore , Toast.LENGTH_LONG).show();
levelFailed = getResources().getStringArray(R.array.levelFailed);
levelSuccess = getResources().getStringArray(R.array.levelSuccess);
highScoreText = (TextView) findViewById(R.id.txtHighScore);
startTimer();
}
private void startTimer(){
start = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeText = (TextView) findViewById(R.id.textTime);
timeText.setText("Zaman : " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
timeText = (TextView) findViewById(R.id.textTime);
timeText.setText("Süre Doldu ! ");
handler.removeCallbacks(runnable);
for (ImageView image : imageArray) {
image.setVisibility(View.INVISIBLE);
}
int x = 0;
if (score < 10){
showFailedAlertDialog(R.layout.my_failed_dialog);
}
else if (score >= 10){
showAlertDialog(R.layout.my_success_dialog);
highScore();
}
}
}.start();
}
@SuppressLint("SetTextI18n")
public void highScore(){
SharedPreferences myScore = getSharedPreferences("my_pref",MODE_PRIVATE);
if (highScore > score) {
highScoreText.setText("Highscore: " + highScore);
} else {
highScore = score;
highScoreText.setText("Highscore: " + highScore);
myScore.edit().putInt("high_score_level1", highScore).apply();
user = FirebaseAuth.getInstance().getCurrentUser();
//Upload user data
if (isOnline() && user != null && user.getDisplayName() != null) {
final DatabaseReference refDirect = FirebaseDatabase.getInstance().getReference();
refDirect.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
refDirect.child("Users" + "/" + user.getUid() +
"/name").setValue(user.getDisplayName());
refDirect.child("Users" + "/" + user.getUid() +
"/image").setValue(user.getPhotoUrl().toString());
refDirect.child("Users" + "/" + user.getUid() + "/highscore").setValue(highScore);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
public void showAlertDialog(int my_success_dialog){
builderDialog = new AlertDialog.Builder(this);
View layoutView = getLayoutInflater().inflate(R.layout.my_success_dialog,null);
if (mInterstitialAd != null) {
mInterstitialAd.show(Main2Activity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
randomArray = (TextView) layoutView .findViewById(R.id.successMessage);
randomArray.setText("");
successText = (TextView) layoutView .findViewById(R.id.txtSuccessScore);
successText.setText("Skor: " + score);
highScoreText = (TextView) layoutView .findViewById(R.id.txtHighScore);
highScoreText.setText("Highscore: " + highScore);
int randomIndex = new Random().nextInt(levelSuccess.length);
String randomName = levelSuccess[randomIndex];
randomArray.setText(randomName);
AppCompatButton dialogButton = layoutView.findViewById(R.id.successAnaMenu);
AppCompatButton dialogButtonTwo = layoutView.findViewById(R.id.successNextLevel);
builderDialog.setView(layoutView);
builderDialog.setCancelable(false);
alertDialog = builderDialog.create();
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
Main2Activity.this.startActivity(intent);
}
});
dialogButtonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), Main5Activity.class);
Main2Activity.this.startActivity(intent);
}
});
}
}
更新:
SharedPreferences myScore = getSharedPreferences("my_pref",MODE_PRIVATE);
totalScore = myScore.getInt("high_score_level2",0);
Toast.makeText(this, "Highscore level2 : "+ totalScore , Toast.LENGTH_LONG).show();
if (highScore > score) {
highScoreText.setText("Highscore: " + highScore + totalScore);
} else {
highScore = score;
highScoreText.setText("Highscore: " + highScore + totalscore);
myScore.edit().putInt("high_score_level1", highScore).apply();
1-共享首选项传递给下一个活动的值 2-Firebase 数据库获得价值,但我失败了
由于数据结构尚未详细说明或检索,因此我提供了一个假设任意键名称的随机示例。请根据您的要求更换。
每当更新分数时,您都需要使用 for 循环计算最高分数。此外,我正在存储高分以供其他用途
final DatabaseReference refDirect = FirebaseDatabase.getInstance().getReference();
refDirect.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String uid = user.getUid();
refDirect.child("Users").child(uid).child("name").setValue(user.getDisplayName());
refDirect.child("Users").child(uid).child("image").setValue(user.getPhotoUrl().toString());
// Assuming you have a list of levels and their respective high scores
List<LevelScore> levelScores = calculateHighScoresForLevels(dataSnapshot);
int totalHighScore = 0;
for (LevelScore levelScore : levelScores) {
refDirect.child("Users").child(uid).child("highscores")
.child(levelScore.getLevelId()).setValue(levelScore.getScore());
totalHighScore += levelScore.getScore(); // Calculate total high score
}
refDirect.child("Users").child(uid).child("totalHighScore").setValue(totalHighScore);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Handle onCancelled if needed
}
});