2

Why is my while loop freezing up my program?

gameLoop() is holding up my other programs processes.

    while (running) {
        if (Key.up) {
            player.moveCharacter(1, playerSpeed);
        } else if (Key.down) {
            player.moveCharacter(2, playerSpeed);
        } else if (Key.left) {
            player.moveCharacter(3, playerSpeed);
        } else if (Key.right) {
            player.moveCharacter(4, playerSpeed);
        }
    }

                        // listen for log in data
        while (true) {
            try {
                int direction = socketIn.readByte(); // player direction
                                                        // sent from server
                int px = socketIn.readByte(); // player px
                int py = socketIn.readByte(); // player py
                String username = socketIn.readUTF(); // player username

                player = new Player(direction, px, py, username); // create
                break;
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
        game.gameLoop(player);

game.gameLoop(player); calls my game loop as you can see, however when it's run my game seizes to continue it's other processes. Can anybody tell me why this is happening? Do I need to put it on a thread?

2
  • Because you are in a continuous loop in gameLoop, ie: while (true) without break condition. Commented Jan 17, 2014 at 17:00
  • I have updated my code. Still doesn't work. Commented Jan 17, 2014 at 17:10

5 Answers 5

1

You are using while(true), and the only way to go out of the loop is with a break, but if you have that code on the same thread than the thread which manage the GUI, your GUI will freeze and the user will not be able to log in because the GUI is frozen by that loop.

The solution is to set that code on another thread different than the main thread (which manage the GUI).

Sign up to request clarification or add additional context in comments.

Comments

0

You use while(true) which will never, ever be false. Thus, the loop will never break and your code will continue to repeat it forever.

2 Comments

You need to give an actual condition instead of just the word true, under what condition(s) would you like that loop to break?
Again, under what condition(s) would you like that loop to break? I think you have a very, very fundamental misunderstanding of how loops work.
0

You're using while (true), so it will continuously check the key state and execute the code blocks. Instead check them every several seconds (or milliseconds)

Comments

0

yes. If you need to stay in the loop for the whole game (after login) then you need to put it in its own thread.

Comments

0

I know i'm a few years late for this, but hope it helps anyone reading

you can use a flag for your while-loop condition

flag = true;
while(flag){

    //some code here

    if(condition)
        flag  = false;
}

the if condition is the condition to exit the loop, i.e. when you break out of it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.