java如何在新游戏《刽子手》中重置记忆?
我正在做一个刽子手游戏。游戏正常运行,只是当我开始一个新游戏时,我无法清除前一个游戏中存储的数据。因此,在用户单击“再次播放”后,它会给我一个如下所示的屏幕
Screenshot of Hangman Game - How it should look after clicking "play again"
Screenshot of Hangman Game after clicking "play again"
如何重置数据,以便执行startNewName方法后的textview和按钮
试图使用文本视图。setText(“”)执行重置,但它不工作。如果你能帮我解释一下,我将不胜感激。谢谢
我的代码如下
package com.desmondwong.hangmangame;
import 安卓.annotation.SuppressLint;
import 安卓.app.AlertDialog;
import 安卓.content.DialogInterface;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.animation.Animation;
import 安卓.view.animation.AnimationUtils;
import 安卓.widget.Button;
import 安卓.widget.GridLayout;
import 安卓.widget.ImageSwitcher;
import 安卓.widget.ImageView;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import 安卓.widget.ViewSwitcher;
import java.util.Random;
public class GameActivity extends AppCompatActivity {
//To reference the components
ImageSwitcher imageSwitcher;
TextView textView;
TextView textViewScore;
Button btn [] = new Button[26];
//Images for the hangman
int img [] = {R.drawable.img0,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8};
AlertDialog helpAlert;
//Variables
String strSecret = "", strGuess="", strText="";
String strWords[] = {"APPLE", "ORANGE","BANANA"};
int intError = 0; //Error made by player
int livesRemaining = 8; //Lives remaining by player
int numCorr = 0; //Correct letter guess by player
Random random = new Random(); //Random generator
public void startNewGame(){
intError = 0; //Error made by player
livesRemaining = 8; //Lives remaining by player
numCorr = 0; //Correct letter guess by player
imageSwitcher.removeAllViews();
//textViewScore.setText(String.valueOf(livesRemaining));
//textView.setText("");
setupImageSwitcher();
setup26Buttons();
getSecretWord();
}
//To create help icon at top right
@Override
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);
return true;
}
//To create help icon at top right
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//case 安卓.R.id.home:
// NavUtils.navigateUpFromSameTask(this);
//return true;
case R.id.action_help:
showHelp();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
//Retrieve the reference
imageSwitcher = findViewById(R.id.imageSwitcher);
textView = findViewById(R.id.textView);
textViewScore = findViewById(R.id.textViewScore);
textViewScore.setText(String.valueOf(livesRemaining));
setupImageSwitcher();
setup26Buttons();
getSecretWord();
}
private void setup26Buttons() {
GridLayout g = findViewById(R.id.gridLayout);
//to create 26 buttons
for(int i = 0; i<btn.length; i++) {
btn[i] = new Button(this, null, R.attr.buttonStyleSmall); //Buttonsytlesmall so that it fits the screen
btn[i].setText(""+(char)('A'+i)); //need to set back to char, as +i will set it back to integer. "" to set this to a String so it is sync to setText
btn[i].setTag(""+(char)('A'+i));
btn[i].setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v) {
strGuess += v.getTag(); //Get letter that the player guessed and keep adding on to strGuess
v.setEnabled(false); //disable pressed button since the player already press
v.setBackgroundColor(安卓.R.color.black);
//Check for error guess. If the letter is not inside the strSecret, it will return less than 0
if (strSecret.indexOf(v.getTag().toString())<0){
intError++; //your error is added
int livesRemaining = 8;
livesRemaining -= intError; // Countdown based on errors recorded
textViewScore.setText(String.valueOf(livesRemaining));
imageSwitcher.setImageResource(img[intError]); //set the img no. to follow the error
}
else {
numCorr++;
}
boolean playerWin = true;
//Display all correct guesses
strText = ""; //reset the display
for (int i = 0 ; i<strSecret.length();i++){
char ch = strSecret.charAt(i); // get each character from strSecret
//To check if this letter can be found in strGuess
if(strGuess.indexOf(ch)>=0){
//found
strText += ch;
}
else{
//Not found
strText += "-";
playerWin=false;
}
}
textView.setText(strText);
if (playerWin) {
if (numCorr == strSecret.length()) {
//Toast.makeText(getApplicationContext(), "You won", Toast.LENGTH_LONG).show();
//let user know they have won, ask if they want to play again
AlertDialog.Builder winBuild = new AlertDialog.Builder(GameActivity.this);
winBuild.setTitle("Amazing! You save Batman!");
winBuild.setMessage("The Hangman\'s favourite fruit is:\n\n" + strSecret);
winBuild.setPositiveButton("Play Again",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.startNewGame();
}
});
winBuild.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}
});
winBuild.show();
}
} else if (livesRemaining > 0) {
//still have lives remaining. do nothing
}
else {
//Toast.makeText(getApplicationContext(), "You Lost",Toast.LENGTH_LONG).show();
// Display Alert Dialog
AlertDialog.Builder loseBuild = new AlertDialog.Builder(GameActivity.this);
loseBuild.setTitle("Batman got executed!");
loseBuild.setMessage("You lose!\n\nThe answer was:\n\n"+ strSecret);
loseBuild.setPositiveButton("Play Again",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.startNewGame();
}});
loseBuild.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();
}});
loseBuild.show();
}
}
});
g.addView(btn[i]);
}
}
private void getSecretWord() {
int index = random.nextInt(strWords.length);
strSecret = strWords[index];
for(int i=0; i<strSecret.length(); i++) {
strText += "-"; //to create multiple - for the unknown word
}
textView.setText(strText);
}
private void setupImageSwitcher() {
//https://www.tutorialspoint.com/安卓/安卓_imageswitcher.htm
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.img0);
return imageView;
}
});
Animation aniOut = AnimationUtils.loadAnimation(this, 安卓.R.anim.slide_out_right);
Animation aniIn = AnimationUtils.loadAnimation(this, 安卓.R.anim.slide_in_left);
imageSwitcher.setOutAnimation(aniOut);
imageSwitcher.setOutAnimation(aniIn);
}
//show help information
public void showHelp(){
AlertDialog.Builder helpBuild = new AlertDialog.Builder(this);
helpBuild.setTitle("Help");
helpBuild.setMessage("Whisper the password (Hangman's favourite fruit) to save Batman\n\n"
+ "You only have 8 tries!");
helpBuild.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
helpAlert.dismiss();
}});
helpAlert = helpBuild.create();
helpBuild.show();
}
}
# 1 楼答案
你的问题是每次打电话给
startNewGame
你:strText
的现有值,而不是设置新文本btn
数组中添加新按钮(不删除旧按钮)因此,你需要确保
startNewGame
摆脱为上一个游戏创建的、你不再需要的所有东西编辑
一种方法是让一个“游戏”对象存储所有东西(strescret、strGuess等)而不是让他们参加活动。 每次调用
startNewGame
时,您都会执行myGame = new Game();
,因此您可以确定之前的所有数据都已丢失编辑2
快速解决方法是:
编辑3(工作改进示例)
主要活动
游戏课