Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android studio:检查对象状态_Java_Android Studio_While Loop - Fatal编程技术网

Java Android studio:检查对象状态

Java Android studio:检查对象状态,java,android-studio,while-loop,Java,Android Studio,While Loop,我有一个游戏,在屏幕上产生4个敌人从图像框,当计时器达到零,一个敌人在屏幕上产生。我想要一种不断检查敌人是否在屏幕上可见的方法 我的类当前包含用于检查可见性的while循环: public class rear_gunner extends AppCompatActivity { int clickcount=0; CountDownTimer countDownTimer; int score = 0; int health2 = 100; @Override protected vo

我有一个游戏,在屏幕上产生4个敌人从图像框,当计时器达到零,一个敌人在屏幕上产生。我想要一种不断检查敌人是否在屏幕上可见的方法

我的类当前包含用于检查可见性的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();



}
}

感谢您的帮助,如果您能提供更有效的建议,我们将不胜感激

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

您的while循环永远不会运行,如果您想要刷新UI,您必须在另一个地方编写此代码,可能是后台计时器任务线程,或者使用
处理程序来处理此问题。

有关如何执行此操作的任何帮助或资源?