这是我原始问题的编辑,因为我改变了我想要显示分数的方式。
以前,我从GameActivity类得到的分数会传递给MainMenu活动类没问题。我的问题是我想要显示一个高分,如果用户击败了之前的最佳状态,这将会改变。
我现在改变了主意,因为我会在游戏结束时显示游戏活动中的用户当前得分,当他们点击返回MainMenu班级/屏幕时,只会显示最高得分。所以,我稍微更改了代码,只显示了最高分,但这只是显示了之前游戏的得分,无论它是否得分更高。
我需要知道如何制作它,以便只有当它击败前一个时才将得分传递给MainMenu活动。
我的GameActivity课程如下:
public class GameActivity extends Activity {
GameView gameView;// Reference the gameView
MediaPlayer music;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the layout
setContentView(R.layout.game_view_container);
// Get the id of the layout
RelativeLayout mainscreen = (RelativeLayout) findViewById(R.id.mainscreen);
// Make the GameView
gameView = new GameView(this);
// Get data from intent and config gameView here
music = MediaPlayer.create(this, R.raw.backgroundmusic);
music.start();
gameView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
// Add GameView
mainscreen.addView(gameView);
}
/* Called when activity is done and should be closed.
* The ActivityResult is propagated back to whoever launched via onActivityResult()
*/
public void finish(){
Intent returnIntent = new Intent();
returnIntent.putExtra("GAME_SCORE", gameView.getHitCount());
//returnIntent.putExtra("HIGH_SCORE", gameView.getHitCount());
setResult(RESULT_OK, returnIntent);
music.release();
super.finish();
}
private Activity getActivity() {
return null;
}
}
我的MainMenu课程如下:
public class MainMenu extends Activity {
private static final int SCORE_REQUEST_CODE = 1;// The request code for the intent
SharedPreferences prefsScore;
//TextView tvScore;
String score;
Intent gameIntent;
TextView highestScore;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_start);
//tvScore = (TextView) findViewById(R.id.tvSpriteGame);
highestScore = (TextView) findViewById(R.id.tvHighestScore);
}
private Activity getActivity() {
return null;
}
public void startGame(View v) {
gameIntent = new Intent(this, GameActivity.class);
startActivityForResult(gameIntent, SCORE_REQUEST_CODE);
}
/* 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;
}
@Override
// 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.userInfo:
Intent intentSettings = new Intent(this, SettingsActivity.class);
startActivity(intentSettings);
return true;
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.scores:
Intent intentScore = new Intent(this, ScoresActivity.class);
startActivity(intentScore);
return true;
case R.id.settings:
Toast.makeText(this, "Game Settings chosen", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Intent intent = new Intent(this, HelpActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
// 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");
highestScore.setText(Integer.toString(scoreFromGame));
/*if (retIntent.hasExtra("HIGH_SCORE")) {
SharedPreferences.Editor editor = sharedPref.edit();
int highestScore = retIntent.getExtras().getInt("HIGH_SCORE");
highScore.setText((Integer.toString(scoreFromGame)));
if (scoreFromGame > highestScore) {
editor.putInt(getString(R.string.score), scoreFromGame);
editor.commit();
} else {
highScore.setText("" + highestScore);
}
}*/
}
}
}
}
}
答案 0 :(得分:0)
对于您来说,存储最高价值的最佳和最简单的方法是共享偏好。它们是应用程序活动中持久的关键值对,甚至在之后。
用法:
https://developer.android.com/training/basics/data-storage/shared-preferences.html