2016-10-03 32 views
0

我是新来的Android应用程序开发时,如何正确地等待,我想创建一个简单的西蒙说游戏的按钮。现在,当我显示序列时,将按钮颜色设为白色,尝试(并且当前失败)等待500毫秒,然后将按钮颜色恢复为原始颜色。重新着色按钮

的问题是,无论怎样,我等待期间不工作,我如何期望,并导致多个按钮是白色的在同一时间,或崩溃的应用程序,没有例外,我可以在控制台中看到。我尝试过使用Handler.postDelayed(),现在我正在尝试使用一个Timer对象,它实际上并不会完全改变颜色,并且会在创建新游戏后崩溃。

这里是我当前的Java代码:

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

做到这一点,

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

个人而言,我会用AnimationAnimationListener在一起,onAnimationEnd我会继续做我的游戏逻辑。

0

给你需要500毫秒后更改按钮的颜色..

 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

我编辑了我的代码,第二个函数将在1s后调用...根据您的需要更改时间... –