Hi,
I have been trying to create a game loop that is paused when a certain key is pressed. This seemed to work ok in Swing but in javafx the loop locks up the UI, even if i create a seperate thread.
@FXML
public void setPaused(KeyEvent e) {
if (e.getCode().compareTo(KeyCode.P) == 0) {
//isPaused = !isPaused; Ignored for now
drawLoop();//begin draw loop
}
}
//used to create an interactive loop on the canvas
public void drawLoop() {
while(!isPaused){
redraw();
try{
Thread.sleep(17);
}catch(){
}
}
}
public long redraw() {
long t = System.currentTimeMillis();
width = getWidth();
height = getHeight();
drawTiles(); //simple draws images onto a canvas, basically a drawImage(buffer) function
return System.currentTimeMillis() - t;
}
This is an extremely cutback version of what i have in full ( i had threads at one point for the redraw function, same problem), but ultimately from doing reading iv found that Javafx uses the event dispatcher to handle the UI and apparently calling a loop from a button press locks this thread up...to my understanding i could be wrong.
Can anyone recommend an alternative way to achieve a simple redraw loop that wont freeze my UI in a loop and can be triggered and stop via keyboard? Any help would be appreciated :)