2016-10-03 5 views
0

Android 앱 개발에 익숙하지 않고 단추가있는 간단한 Simon Says 게임을 만들려고합니다. 바로 지금 버튼을 흰색으로 채우는 시퀀스를 표시하면 500ms를 기다린 다음 (그리고 현재 실패) 버튼을 원래 색상으로 되돌립니다.버튼을 다시 칠 때 제대로 대기하는 방법

문제는 내가 기다리는 방식이 여러 가지 버튼을 동시에 흰색으로 예상하거나 작동하지 않게하거나 콘솔에서 볼 수있는 예외없이 응용 프로그램을 중단시키는 것입니다. Handler.postDelayed()를 사용해 보았습니다. 이제는 실제로 색상 변경 작업을 수행하지 않는 Timer 객체를 사용하려고합니다. 새 게임을 만든 후 충돌이 발생합니다.

t.schedule(new TimerTask() { 
    @Override 
    public void run() { 
     runOnUiThread(new Runnable(){ 
      buttons[i].setBackgroundColor(colors[i]); 
     }); 
    } 
}); 

는 개인적으로 내가 Animation 함께 AnimationListener, onAnimationEnd 내가 내 게임 로직을하고 다시 시작합니다 사용하는 것이,

package mpeterson.SimonSays; 

    import android.graphics.Color; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import java.util.*; 


    public class MainActivity extends AppCompatActivity { 
     private ArrayList<Integer> sequence; 
     private int curPos; 
     private boolean waiting; 
     private Random rand; 
     private Button start; 
     private Button[] buttons = new Button[4];//green,red,blue,yellow 
     private int[] colors = {Color.GREEN, Color.RED, Color.BLUE, Color.YELLOW}; 
     private Timer t = new Timer(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     buttons[0] = (Button) findViewById(R.id.green); 
     buttons[1] = (Button) findViewById(R.id.red); 
     buttons[2] = (Button) findViewById(R.id.blue); 
     buttons[3] = (Button) findViewById(R.id.yellow); 
     start = (Button) findViewById(R.id.start); 
     //System.out.println("Initialized"); 
    } 
    public void startGame(View view){ 
     sequence = new ArrayList<>(); 
     curPos = 0; 
     rand = new Random(); 
     waiting = false; 
     start.setText("Restart Game"); 
     for(Button b : buttons) { 
      b.setVisibility(View.VISIBLE); 
     } 
     try{ 
      //System.out.println("to nextInSeq"); 
      nextInSeq(); 
     } 
     catch(InterruptedException e){ 
      e.printStackTrace(); 
     } 
    } 
    public void nextInSeq() throws InterruptedException { 
     //System.out.println("inseq"); 
     if (waiting) return; 
     sequence.add(rand.nextInt(4)); 
     //System.out.println(next); 
     for (final int i : sequence) { 
      buttons[i].setBackgroundColor(Color.WHITE); 
      t.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        buttons[i].setBackgroundColor(colors[i]); 
       } 
      }, 0, 500); 
     } 
     waiting = true; 
     curPos = 0; 
    } 
    public void makeMove(View view) throws Exception{ 
     //System.out.println("here"); 
     if(!waiting) return; 
     int move = Integer.parseInt(view.getTag().toString()); 
     //System.out.println("move " + move); 
     if(sequence.get(curPos) == move){ 
      //System.out.println("Correct move: " + curPos); 
      curPos++; 
      if(curPos == sequence.size()){ 
       try{ 
        waiting = false; 
        nextInSeq(); 
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
     else { 
      for(Button b : buttons){ 
       b.setVisibility(View.INVISIBLE); 
      } 
      start.setText("New Game"); 
     } 
    } 
} 

답변

0

이를 수행

여기에 내 현재의 Java 코드입니다.

0

당신은 500ms 후 변경해야 할 버튼의 색상을 지정 ..

 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      // this will show after 500ms 
     } 
    }, 500); 

이 시도 :

Button button; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button); 
    call(); 
} 
public void call(){ 
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      button.setBackgroundColor(Color.GREEN); 

      call2(); 
     } 
    }, 1000); 

} 
public void call2(){ 
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      button.setBackgroundColor(Color.WHITE); 

     } 
    }, 1000); 
} 
+0

나는이 시도하고 응용 프로그램이 실제로 지금 실행,하지만 난 여전히으로 실행하고 동시에 여러 버튼이 흰색이 될 수 있습니다. 핸들러를 다시 호출하기 전에 기다려야합니까? –

+0

내 코드를 편집했습니다. 두 번째 함수가 1 초 후에 호출됩니다 ... 필요에 따라 시간을 변경하십시오 ... –

관련 문제