Don't see why this loop does not get reached

82 Views Asked by At

Because of many boolean checks i am a little confused, let me introduce me to my next code.

        } 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();
        }
    }
}

This else statement can be seen as the first time a player gets hit by an enemy missile. To make an explosion-animation. I need te screen to freeze for 2 seconds and then start the game over again. I don't see why the second if statement is never reached. Here are my ontouch methods and my new game method:

    @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);
}

As one can see, the new game just resets the players position, the only thing i want to do is just freeze the screen for 2 seconds before that happens, this is supposed to happen in the second if statement but it never get reached...

Any suggestions?

ps: deadTimepassed is introduced as 'private long' just beneath the public class method.

1

There are 1 best solutions below

2
On

For freeze your Thread (so, the program will be freeze, also your screen) during 2 seconds you can use Thread.sleep() function:

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

You have to put it inside a try-catch block because it can gives to you an InterruptedException.

I expect it will be helpful for you!