java for循环延迟_如何在java中的for循环中延迟方法?

我在for循环中延迟方法assign_backgrounds()时遇到问题.我正在尝试创建一个西蒙说的游戏,但不是延迟并显示“西蒙”按下的下一个按钮,它会立即显示所有按钮.这里的任何帮助将不胜感激.谢谢.

boolean simonsTurn = true;

int x = 4;

int s;

int delay = 1000;

int array_values[] = new int[]{1,2,3,4};

public void simonSays() {

// running = true;

if (simonsTurn == true) {

go();

for (int i = 0; i < x; i++) {

Handler handler = new Handler();

handler.postDelayed(new Runnable() {

public void run() {

go();

}

}, 1000);

}

}

}

public void go(){

s = random_int_between(0,3);

assign_backgrounds(array_values[s]);

}

public void assign_backgrounds( int array_values ){

Handler handler = new Handler();

if( array_values == 1){

button1_.invalidate();

button1_.setBackgroundResource(R.drawable.goatclicked);

button1_.refreshDrawableState();

handler.postDelayed(new Runnable(){

public void run(){

button1_.invalidate();

button1_.setBackgroundResource(R.drawable.goat);

button1_.refreshDrawableState();}}, 1000);

}

else if( array_values == 2){

button2_.invalidate();

button2_.setBackgroundResource(R.drawable.pigclicked);

button2_.refreshDrawableState();

handler.postDelayed(new Runnable(){

public void run(){

button2_.invalidate();

button2_.setBackgroundResource(R.drawable.pig);

button2_.refreshDrawableState();}}, 1000);

}

else if( array_values == 3){

button3_.invalidate();

button3_.setBackgroundResource(R.drawable.chickenclicked);

button3_.refreshDrawableState();

handler.postDelayed(new Runnable() {

public void run() {

button3_.invalidate();

button3_.setBackgroundResource(R.drawable.chicken);

button3_.refreshDrawableState();}}, 1000);

}

if( array_values == 4) {

button4_.invalidate();

button4_.setBackgroundResource(R.drawable.cowclicked);

button4_.refreshDrawableState();

handler.postDelayed(new Runnable(){

public void run(){

button4_.invalidate();

button4_.setBackgroundResource(R.drawable.cow);

button4_.refreshDrawableState();}}, 1000);

}

}

你可能感兴趣的:(java,for循环延迟)