2017-01-24 23 views
0

即時通訊新的android工作室,我想這個函數循環,並創建一個無限的球落環將被評分,直到兩個整數不匹配,但是當我嘗試運行它,它只會循環一次,如果我在else語句更改streak = 2應用程序崩潰我如何組織這個功能,所以它的作品,我打算它

public void mainGameLoop(){ 
    do { 
     //sets Balls integer 
     setColourint(); 
     ball.setVisibility(View.VISIBLE); 
     //ball fall 
     float bottomOfScreen = getResources().getDisplayMetrics() 
       .heightPixels - (ball.getHeight() * 4); 

     //fall animation 
     ball.animate() 
       .translationY(bottomOfScreen) 
       .setInterpolator(new AccelerateInterpolator()) 
       .setInterpolator(new BounceInterpolator()) 
       .setDuration(9000); 
     //once animation is complete compares balls variable with current variable 
     if (colourint == ranint){ 
      //if they are same then +1 score 
      score = score+1; 
      scr.setText(Integer.parseInt(String.valueOf(score))); 

     } else { 
      //else game is over 
      streak = 2; 
     } 
     //repeat until game is over 
    } while (streak == 1); 
} 

一旦球達到相對佈局我想要的功能檢查的底部,如果setcoulour intran int是相同的,然後如果是score = score + 1,球回到頂部,setColourInt函數被調用並且球再次落下(等等等等),但如果不是循環結束,它的遊戲結束了......我爲我公然的無能而道歉,但我無法想象你們也不記得你們什麼時候也是,在編碼時這是天真的。非常感謝湯姆

+0

現在你認爲「錯了」是什麼? – markspace

回答

0

你必須讓用戶界面有機會畫出你在做什麼。您使用它編碼的方式,調用mainGameLoop的任何方法(onStartonCreate等)將永遠不會返回,因爲它陷入了無限循環。這是一個錯誤。

一種方法是創建一個View並在onDraw方法中實現您的遊戲繪製邏輯。當需要繪製時,UI線程會調用您的onDraw方法。這裏有一篇關於這種方法的文章:http://cjds.github.io/2014/04/28/Creating-a-simple-android-game/

相關問題