Thread only running correctly if there is a System.out.println() inside the while true loop

后端 未结 5 2011
礼貌的吻别
礼貌的吻别 2021-01-06 14:38

Basicly, I\'m making a game which to update the players position, it uses this thread:

@Override
public void run() {
    while(true) {
        System.out.pri         


        
5条回答
  •  星月不相逢
    2021-01-06 15:20

    Don't know if that would solve it, but it looks like you're giving the JVM a hard time... Try adding a short sleep (which is the correct way to implement a game thread) before each updatePos(x, y) call.

    @Override
    public void run() {
        while(true) {
        Try
        {
            Thread.sleep(50);
        }
        catch (Exception e){}
    
        updatePos(x, y);
        }
    

    }}

提交回复
热议问题