2017-03-28 77 views
0

我有一個遊戲,在屏幕上從圖像框中產生4個敵人,當計時器達到零時,敵人會產生到屏幕上。我想要一種方法來不斷檢查敵人是否可見或不在屏幕上。Android工作室:檢查對象狀態

我目前包含while循環類檢查可見:

public class rear_gunner extends AppCompatActivity { 

int clickcount=0; 
CountDownTimer countDownTimer; 
int score = 0; 
int health2 = 100; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rear_gunner); 

    final TextView text = (TextView) findViewById(R.id.textView2); 
    final TextView scoreText = (TextView) findViewById(R.id.score); 
    final TextView health = (TextView) findViewById(R.id.Health); 
    health.setText("Health:"+ health2); 
    //Enemy ImageViews 
    final ImageView enemy1 = (ImageView) findViewById(R.id.enemy1); 
    final ImageView enemy2 = (ImageView) findViewById(R.id.enemy2); 
    final ImageView enemy3 = (ImageView) findViewById(R.id.enemy3); 
    final ImageView enemy4 = (ImageView) findViewById(R.id.enemy4); 

    //sets screen orientation on created 
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    //sets imageViews into array 
    final ImageView[] enemies = new ImageView[4]; 
    enemies[0] = enemy1; 
    enemies[1] = enemy2; 
    enemies[2] = enemy3; 
    enemies[3] = enemy4; 

    boolean running = true; 
    while (!running) { 
     if (enemy1.getVisibility() == View.VISIBLE) { 
      int damage = 1; 
      health2 = health2 - damage; 
      health.setText("Health:" + health2); 
     } else { 
      // Either gone or invisible 
     } 
     if (enemy2.getVisibility() == View.VISIBLE) { 
      int damage = 1; 
      health2 = health2 - damage; 
      health.setText("Health:" + health2); 
     } else { 
      // Either gone or invisible 
     } 
     if (enemy3.getVisibility() == View.VISIBLE) { 
      int damage = 1; 
      health2 = health2 - damage; 
      health.setText("Health:" + health2); 
     } else { 
      // Either gone or invisible 
     } 
     if (enemy4.getVisibility() == View.VISIBLE) { 
      int damage = 1; 
      health2 = health2 - damage; 
      health.setText("Health:" + health2); 
     } else { 
      // Either gone or invisible 
     } 
    } 

上面的代碼只是根本不起作用。當我開始活動時,屏幕會變黑,直到關閉爲止。這與我使用while循環進行運行的方式有什麼關係?如果需要的話

我的計時器代碼:

//random number generator between 10-25 
    Random r = new Random(); 
    int Low = 10000; 
    int High = 25000; 
    int Result = r.nextInt(High-Low) + Low; 

    //count down timer for spawing enemies 
    countDownTimer = new CountDownTimer(Result, 1000) { 

     @Override 
     public void onTick(long millisUntilFinished) { 
      text.setText("seconds remaining: " + millisUntilFinished/1000); 

      if (score == 500) 
      { 
       countDownTimer.cancel(); 
      } 
     } 
     @Override 
     public void onFinish() { 
      Random r = new Random(); 
      int Low = 0; 
      int High = 4; 
      int Result = r.nextInt(High-Low) + Low; 
      enemies[Result].setVisibility(View.VISIBLE); 

      countDownTimer.start(); 
      if (score == 500) 
      { 
       countDownTimer.cancel(); 
      } 
     } 



    }; 




    countDownTimer.start(); 



} 

}

謝謝所有幫助和這樣做的更有效的方式任何建議將大大appriciated。

回答

1
boolean running = true; 
while (!running) { 
... 

while循環永遠不會有機會運行,如果你想刷新UI,你必須編寫這些代碼在另一個地方,也許後臺定時任務線程或使用Handler來處理這個問題。

+0

任何幫助或資源如何做到這一點? –