Code hangs the window

-1

I have this code in my game and check if it has passed 400 milliseconds, and then it updates.

public void setUpdatePosition(int posicao, long tempo, int posX, int posY) {

    long previous = System.nanoTime();
    Point ponto = new Point(tiles[posicao].x, tiles[posicao].y);
    tiles[posicao].setPosition(posX, posY);

    while(((System.nanoTime() - previous) / 1000000) < tempo) {
        previous = System.nanoTime();
    }
    tiles[posicao].setPosition(ponto.x, ponto.y);
}

But it ends up locking my game, how do I solve it?

    
asked by anonymous 27.09.2017 / 22:16

1 answer

0

I solved it this way, but I do not know if it is correct.

public void setUpdatePosition(int posicao, long tempo, int posX, int posY) {

        Point ponto = new Point(tiles[posicao].x, tiles[posicao].y);
        tiles[posicao].setPosition(posX, posY);

        thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(tempo);
                    tiles[posicao].setPosition(ponto.x, ponto.y);
                } catch (InterruptedException e) {}
            }

        });

        thread.start();
    }
    
27.09.2017 / 23:17