不知道为什么没有达到这个循环

时间:2015-06-15 19:22:03

标签: java if-statement boolean

由于许多布尔检查我有点困惑,让我向我介绍下一个代码。

        } else {
        //check whether the player starts again. the first time we lose the player
        //will definitely get into this loop
        if(!startAgain){
            // we only do this to set a timer, we don't want to start a new game so we set this
            // to false
            newgame = false;
            startAgain = true;
            deadTime = System.nanoTime();
        }
        deadTimepassed = (System.nanoTime() - deadTime)/1000000;
        if((deadTimepassed < 2000) && (newgame = false)){
            newGame();
        }
    }
}

这个其他声明可以看作是玩家第一次被敌人的导弹击中。制作爆炸动画。我需要te屏幕冻结2秒钟,然后重新开始游戏。我不明白为什么永远不会达到第二个if语句。这是我的ontouch方法和我的新游戏方法:

    @Override
public boolean onTouchEvent(MotionEvent event) {

    //What happens when the player touches the screen!

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        // Boolean check for when player is playing, if he isn't the chopper does nothing.
        // we see that the chopper only hangs still if all these booleans are true!
        if ((!player.isPlaying()) && (newgame = true) && (startAgain = true)) {
            player.setPlaying(true);

        } if(player.isPlaying()) {
            player.setUp(true);
            startAgain = false;
        }
        return true;
    }

    if (event.getAction() == MotionEvent.ACTION_UP) {
        player.setUp(false);
        return true;
    }
    return super.onTouchEvent(event);
}

    public void newGame(){
    newgame = true;
    missles.clear();
    if(player.getScore()>best) {
        best = player.getScore();
        SharedPreferences.Editor editor = saveBest.edit();
        editor.putInt("new best",best);
        editor.commit();
    }
    player.resetScore();
    player.setY(HEIGHT/2);
}

正如人们所看到的,新游戏只是重置了玩家的位置,我唯一要做的就是在此之前将屏幕冻结2秒钟,这应该发生在第二个if语句中,但它永远不会得到达到...

有什么建议吗?

ps:deadTimepassed被引入为&#39; private long&#39;就在公共类方法之下。

1 个答案:

答案 0 :(得分:0)

要冻结您的Thread(因此,程序将冻结,也会冻结您的屏幕),您可以在2秒内使用Thread.sleep()功能:

try {
    Thread.sleep(2000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

您必须将其放在try-catch块中,因为它可以为您提供InterruptedException

我希望它会对你有所帮助!