我有一个简单的游戏,你点击一个精灵,增加一个命中计数器。一旦游戏结束,我希望得分能够通过游戏开始的主菜单传递但无法使其发挥作用。这是我的三个不同类的代码,我不知道该在方法中放什么。
游戏活动 -
public void finish(){
Intent returnIntent = new Intent();
returnIntent.putExtra("GAME_SCORE",gameView.getHitCount());
setResult(RESULT_OK, returnIntent);
super.finish();
}
游戏视图 -
public String getHitCount() {
String Score = Integer.toString(hitCount);
return Score;
/*Intent returnIntent = null;
String Result = returnIntent.getExtras().getString("GAME_SCORE");
TextView GameScore = (TextView)findViewById(R.id.tvGuessGame);
GameScore.setText(Result);
return Result;*/
}
主菜单 -
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");
tvScore.setText(Integer.toString(scoreFromGame));
}
}
}
}
答案 0 :(得分:0)
游戏得分为字符串,但在onActivityResult
中被取为int答案 1 :(得分:0)
更改getHitCount()方法的返回类型。
public int getHitCount(){
return hitCount;
}
在onActivityResult中:
tvScore.setText("&#34 + Integer.toString(scoreFromGame));