gpt4 book ai didi

Java Android Basic 游戏 - 使用共享首选项保存高分 - 初学者

转载 作者:行者123 更新时间:2023-11-29 19:53:16 25 4
gpt4 key购买 nike

我是 Java Android 编程的初学者。我正在使用 Eclipse 创建一个基本的 android 游戏,我想保存高分。我被告知要使用共享首选项,所以当我退出游戏并再次打开时,高分会被保存。我已经在我的“主菜单”页面上保存了高分,但对将共享首选项代码放在哪里感到很困惑?它应该在我的 OnCreate 中吗?请查看我的代码并尝试帮助我。我知道这可能很简单,但我已经研究了一段时间。

代码如下:

package cct.mad.lab;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cct.mad.lab.GameView;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.net.Uri;

public class MainMenu extends Activity {

//MediaPlayer backgroundmusic;////////////////////

private static final int SCORE_REQUEST_CODE = 1;// The request code for the intent

private static final int PREFERENCE_MODE_PRIVATE = 0;

TextView tvHighScore;
TextView tvLastScore;
//TextView tvScore
String score;
Intent gameIntent;
int HighNum = 0;
SoundPool sp;
MediaPlayer mp;
int hit = 0;
ImageView crash;
ImageView image;
//int highscore = 0;



protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_start);
tvHighScore = (TextView) findViewById(R.id.tvGuessGame);
tvLastScore = (TextView) findViewById(R.id.LastGameNumb);


}

public void startGame(View v)

{
gameIntent = new Intent(this,GameActivity.class);
startActivityForResult(gameIntent, SCORE_REQUEST_CODE );

mp = MediaPlayer.create(this, R.raw.bgmusic); // Assigns the media player to the bgmusic in the raw flder
mp.setLooping(true); // loops the media player so it continues to play
mp.start(); // starts the media player when the start game button is called
}


/* Create Options Menu */
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}


// Respond to item selected on OPTIONS MENU
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//put data in Intent
case R.id.easy:
Toast.makeText(this, "Easy chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.medium:
Toast.makeText(this, "Medium chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.hard:
Toast.makeText(this, "Hard chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.other:
Toast.makeText(this, "Other chosen", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}


protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {

mp.pause(); // Pauses the Mediaplayer when the game is stopped and returned to the main menu.

// Check which request we're responding to
if (requestCode == SCORE_REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
if (retIntent.hasExtra("GAME_SCORE"))
{
int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");

if (scoreFromGame > HighNum)
{
tvHighScore.setText(Integer.toString(scoreFromGame));
tvLastScore.setText(Integer.toString(scoreFromGame));
HighNum = scoreFromGame;
}

else if (scoreFromGame > HighNum)
{
tvHighScore.setText(Integer.toString(scoreFromGame));
tvLastScore.setText(Integer.toString(scoreFromGame));
}

else if (scoreFromGame < HighNum)
{
tvLastScore.setText(Integer.toString(scoreFromGame));
}
}
}
}

}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.stop(); // Stops the mediaplayer when the game/emulator is exited.
mp.release(); // Stops the mediaplayer when the game/emulator is exited.
}







/*
@Override
public void onSaveInstanceState(Bundle savedInstanceState){ // When activity stops, system calls onSaveInstancState to save info.
//Save the user's current game state
savedInstanceState.putInt("GAME_SCORE", HighNum);
super.onSaveInstanceState(savedInstanceState); //Calls superclass to view the hierachy state
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // Calls the superclass first

if (savedInstanceState != null) {
HighNum = savedInstanceState.getInt("GAME_SCORE");
} else {



}

super.onRestoreInstanceState(savedInstanceState);
}



*/




}

最佳答案

你应该查看 Android Activity lifecycle如果您不完全了解 Activity 的生命周期。该图像演示了您的 Activity 可能处于的状态,以及您调用的用于在这些状态之间切换的函数。

enter image description here

您应该能够在 onPause 中保存您的首选项,并在您的 Activity 类的 onResume 函数中加载首选项。这意味着每次关闭/打开 Activity 时都会保存/加载您的首选项。

关于Java Android Basic 游戏 - 使用共享首选项保存高分 - 初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36937274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com