I\'m trying to write a game in JavaFX but I\'m having a slight issue with it and that\'s the key listeners getting interrupted by other key presses. I\'m using scene.s
I would make a set of all keys currently pressed and just check them in a timer. It doesn't eat many cpu cycles on my old machine. In motion it's 5%, but 0% when no key is pressed.
For a possibly faster solution use an ObservableSet and start and stop actions when a key is added or removed.
package keytest;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class KeyTest extends Application {
@Override
public void start(Stage primaryStage) {
ObservableSet downKeys = FXCollections.observableSet();
Circle circle = new Circle(10);
Pane root = new Pane(circle);
Scene scene = new Scene(root, 500, 500);
scene.setOnKeyPressed(evt->{
downKeys.add(evt.getCode());
});
scene.setOnKeyReleased(evt->{
downKeys.remove(evt.getCode());
});
Timeline timer = new Timeline(new KeyFrame(
javafx.util.Duration.millis(16), ae -> {
downKeys.stream().parallel().forEach(kc -> {
Platform.runLater(() -> {//why?
switch(kc){
case UP: circle.setTranslateY(circle.getTranslateY()-2);
break;
case DOWN: circle.setTranslateY(circle.getTranslateY()+2);
break;
case LEFT: circle.setTranslateX(circle.getTranslateX()-2);
break;
case RIGHT: circle.setTranslateX(circle.getTranslateX()+2);
break;
}
});
});
}));
timer.setCycleCount(Animation.INDEFINITE);
timer.play();
//I think this might be faster
downKeys.addListener((SetChangeListener.Change extends KeyCode> change) -> {
if (change.wasAdded()){
System.out.println("start action "+change.getElementAdded());
}
if (change.wasRemoved()){
System.out.println("stop action "+change.getElementRemoved());
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I'm thinking the parallel is stupid and the Platform.runLater costs more than the threading of +2 or -2 . But hey, java8 that's why