Android studio:检查对象状态

时间:2017-03-28 00:26:27

标签: 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 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();



}

}

感谢您提供所有帮助,任何有关更有效方法的建议都会受到极大关注。

1 个答案:

答案 0 :(得分:1)

radios>input[type="radio"] {
    -webkit-appearance: checkbox;
    -moz-appearance: checkbox;
}

你的while循环永远不会有机会运行,如果你想刷新UI,你必须在另一个地方写这个代码,也许是后台Timer任务线程或使用boolean running = true; while (!running) { ... 来处理这个。