0

I tried to make pauses between moves by using Thread.sleep but it doesn't work properly. When I start demo, the screen just freezes and after a few seconds shows me a resolved puzzle... This is my code:

// starting a demonstration
public void startDemo() {
    ArrayList<Integer> shortestPath = Algorithms.getShortestPath(); // getting the sequence of tile numbers to move
    for (Integer tileNumber: shortestPath) {
        try {
            board.move(tileNumber); // moving a tile
            boardView.invalidate(); // redraw on screen
            Thread.sleep(500); // trying to make a pause between moves
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

I read that blocking UI thread is a bad practice. But what is the best way to resolve this problem?

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149

0 Answers0