2013-03-30 40 views
1

我正在製作一個我們正在製作炸彈人遊戲的學校項目。在比賽場地上有2個炸彈人,用戶和AI。無法使用CountDownTimer類創建處理程序

當用戶放置炸彈並走開時,炸彈在時間結束時閃爍並爆炸。

但是這不適用於AI。

當AI放置了炸彈,並走開了它,當它應閃爍,然後離開這個錯誤發生爆炸的應用程序崩潰:

FATAL EXCEPTION: Timer-0 
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

CountDownTimer代碼如下。

public void explodeAIBomb(){ 


    //Starts countdown 2-4 seconds 

    new CountDownTimer(new Random().nextInt(2000) + 2000, 300) { 
     //test for onTick; bomb flashes 
     boolean test = false; 

    public void onFinish() { 
     //on explosion{ 
     gameBoard[locateAIX][locateAIY] = new Blast();   
     explosionSide(locateAIX, locateAIY); 
     explosionVert(locateAIX, locateAIY); 
     updateView(); 


     //Clear all blasts after 1 second 
     new CountDownTimer(1000, 1000){ 
      public void onFinish(){ 
       for (int x = 0; x < XasLength + 1; x++) { 
        for (int y = 0; y < YasLength + 1; y++) { 
         if(gameBoard[x][y] != null){ 
          if(gameBoard[x][y].getTileId() == BLAST || gameBoard[x][y].getTileId() == BLAST_SIDE || gameBoard[x][y].getTileId() == BLAST_VERT){ 
           gameBoard[x][y] = null; 
          } 
         } 
        } 
       } 
       aiBombPlanted = false; 
       updateView(); 
      } 
      @Override 
      public void onTick(long millisUntilFinished) { 
      }     
     }.start(); 
     updateView(); 
    } 

    //Flashes the bomb every 0.3 second 
    public void onTick(long millisUntilFinished) {    
     if(test){ 
      loadTile(BOMB, r.getDrawable(R.drawable.bomb)); 
      test = false; 
     } 
     else{ 
      loadTile(BOMB, r.getDrawable(R.drawable.bomb2)); 
      test = true; 
     } 
     updateView(); 
    } 
}.start(); 

}

回答

相關問題