0

I'm building a Pacman game, so basically I have to wait for the user to enter a key before moving Pacman. I'm waiting for an input before drawing my new canvas with the movement done, but I'm stuck on the line where I have to wait the entry.

public void something(){
  //do something
  wait until a key is pressed 
  if( key pressed is an arrow key){
    something();
  }else{
    wait for the key to be pressed
  }
}

I've been told to use something like setFocusTraversable(true) and requestFocus(). Is it relevant? Thanks.

EDIT:

public void afficher(Labyrinth labyrinth) {
    Pane root = new Pane() {
        @Override
        protected void layoutChildren() {
            GraphicsContext gc = canvas.getGraphicsContext2D();
            canvas.setWidth(sceneWidth);
            canvas.setHeight(sceneHeight);
            gc.setFill(Color.WHITE);
            gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.setOnKeyPressed((KeyEvent t) -> {
                // I think that's the point of the keyevent
                switch (t.getCode()) {
                    case UP:
                        labyrinth.movePacman(control.Direction.Up);
                        break;
                    case DOWN:
                        labyrinth.movePacman(control.Direction.Down);
                        break;
                    case RIGHT:
                        labyrinth.movePacman(control.Direction.Right);
                        break;
                    case LEFT:
                        labyrinth.movePacman(control.Direction.Left);
                        break;

                }
            });
            drawGame(gc, labyrinth);
        }
    };

    root.getChildren().add(canvas);

    Scene scene = new Scene(root, sceneWidth, sceneHeight);

    primaryStage.setTitle("Pacman");
    primaryStage.setScene(scene);
    primaryStage.setResizable(false); // empeche de modifier la taille de la fenetre
    primaryStage.show();

}

And now for the controller who calls this function

public class ControllerFX extends Application {

Labyrinth labyrinth = new Labyrinth();

@Override
public void start(Stage primaryStage) {
    VueFX vuefx = new VueFX(primaryStage);
    vuefx.afficher(labyrinth);
    do{
        //I think that's the part where I've to be in the loop  
    }while(Labyrinth.getLives() > 0 || Labyrinth.getAmountOfGhosts() > 0);
    Labyrinth.gameOver();
} 

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}
Adem
  • 35
  • 5
  • 1
    Similar QA might help on: http://stackoverflow.com/questions/18037576/how-do-i-check-if-the-user-is-pressing-a-key – Michael Seltene Jan 27 '17 at 18:35
  • I'm not using Swing, thx tho – Adem Jan 27 '17 at 18:37
  • 1
    "I've been told to use something like `setFocusTraversable(true)` and `requestFocus()`. Is it relevant? " Not directly: I think you misunderstood that. You need to listen for key events, as described in the question linked above (note that question is about swing, but the concept is identical), and key events will only be fired on a UI node has the keyboard focus (which is what `requestFocus()` does). But again, you don't *wait* for anything: you have no code running that has to wait. You just write the event handler and register it with something: probably the scene. – James_D Jan 27 '17 at 18:37
  • Thanks mate, i'll try to figure it out with the link above. – Adem Jan 27 '17 at 18:45
  • Actually, that's not really that helpful now I look at the link again. – James_D Jan 27 '17 at 18:46
  • Try http://stackoverflow.com/questions/33224161/how-do-i-run-a-function-on-a-specific-key-press-in-javafx and maybe http://stackoverflow.com/questions/29962395/how-to-write-a-keylistener-for-javafx (the second one is useful if other stuff is happening that needs to be processed even if no keys are pressed) – James_D Jan 27 '17 at 18:48
  • I'll edit with my own code – Adem Jan 27 '17 at 19:16
  • Why do you think you need a loop? Just get rid of the loop entirely. Presumably `Labyrinth` sets its own variables, so it can internally call `gameOver()` when necessary. You also don't need to re-register the event handler every time you lay out the UI (though I don't think it will break anything); just do it once when `canvas` is created. – James_D Jan 28 '17 at 02:03

0 Answers0