我正在创建一个simom说的游戏,如何在代码执行之前设置一个延迟。在图片1中,我需要它等待1秒钟,然后再自动按下按钮。
以下是完整代码:
public class GameActivity extends AppCompatActivity implements View.OnClickListener {
Random rand = new Random();
Handler handler = new Handler(Looper.getMainLooper());
Button btnGreen, btnRed, btnYellow, btnBlue;
TextView tvScore;
MediaPlayer mpSound1, mpSound2, mpSound3, mpSound4;
Animation animation;
boolean gameOver = false;
boolean buttonClicked = false;
String[] fourColors = {"Green", "Red", "Blue", "Yellow"};
ArrayList<String> allColors = new ArrayList<>();
int count = 0;
int scoreToWin = 8;
int i = 0;
public void ClickGreen(){
btnGreen.startAnimation(animation);
mpSound1.start();
}
public void ClickRed(){
btnRed.startAnimation(animation);
mpSound2.start();
}
public void ClickBlue(){
btnBlue.startAnimation(animation);
mpSound3.start();
}
public void ClickYellow(){
btnYellow.startAnimation(animation);
mpSound4.start();
}
public void game(){
count = 0;
i = 0;
int random;
random = rand.nextInt(4);
allColors.add(fourColors[random]);
handler.postDelayed(new Runnable() {
@Override
public void run() {
for(i = 0; i < allColors.size(); i++) {
switch (allColors.get(i)) {
case "Green":
ClickGreen();
Toast.makeText(GameActivity.this, "Green", Toast.LENGTH_SHORT).show();
break;
case "Red":
ClickRed();
Toast.makeText(GameActivity.this, "Red", Toast.LENGTH_SHORT).show();
break;
case "Blue":
ClickBlue();
Toast.makeText(GameActivity.this, "Blue", Toast.LENGTH_SHORT).show();
break;
case "Yellow":
ClickYellow();
Toast.makeText(GameActivity.this, "Yellow", Toast.LENGTH_SHORT).show();
break;
}
}
}
}, 1000);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_down);
tvScore = findViewById(R.id.tvScore);
btnGreen = findViewById(R.id.btnGreen);//0
btnRed = findViewById(R.id.btnRed);//1
btnBlue = findViewById(R.id.btnBlue);//2
btnYellow = findViewById(R.id.btnYellow);//3
btnGreen.setOnClickListener(this);
btnRed.setOnClickListener(this);
btnBlue.setOnClickListener(this);
btnYellow.setOnClickListener(this);
mpSound1 = MediaPlayer.create(this, R.raw.sound1);
mpSound2 = MediaPlayer.create(this, R.raw.sound2);
mpSound3 = MediaPlayer.create(this, R.raw.sound3);
mpSound4 = MediaPlayer.create(this, R.raw.sound4);
game();
}
@Override
public void onClick(View v) {
if(v.equals(btnGreen)) {
btnGreen.startAnimation(animation);
mpSound1.start();
gameOver = !allColors.get(count).equals("Green");
count++;
buttonClicked = true;
}
else if(v.equals(btnRed)) {
btnRed.startAnimation(animation);
mpSound2.start();
gameOver = !allColors.get(count).equals("Red");
count++;
buttonClicked = true;
}
else if(v.equals(btnBlue)) {
btnBlue.startAnimation(animation);
mpSound3.start();
gameOver = !allColors.get(count).equals("Blue");
count++;
buttonClicked = true;
}
else if(v.equals(btnYellow)) {
btnYellow.startAnimation(animation);
mpSound4.start();
gameOver = !allColors.get(count).equals("Yellow");
count++;
buttonClicked = true;
}
if(buttonClicked){
if (gameOver) {
Intent intent = new Intent(GameActivity.this, GameOverActivity.class);
startActivity(intent);
} else {
if (count >= allColors.size()) {
tvScore.setText(String.valueOf(allColors.size()));
game();
buttonClicked = false;
if(allColors.size() == scoreToWin){
Toast.makeText(GameActivity.this, "WON!", Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
图1图2图3图4
2条答案
按热度按时间8ehkhllq1#
您可以添加如下延迟,以等待1秒:
delay(1000)
pu3pd22g2#
使用下面的代码在android应用程序中添加延迟。