0

I'm trying to code a 2048 game using JavaFX and I'm facing a problem.

@Override
public void start(Stage primaryStage){
    primaryStage.setResizable(false);
    Scene scene = new Scene(firstContent());
    primaryStage.setScene(scene);
    primaryStage.show();
    scene.setOnKeyPressed(new EventHandler<KeyEvent>(){
        @Override
        public void handle(KeyEvent e){
            KeyCode key = e.getCode();
            if((key.equals(KeyCode.UP))){
                System.out.println("recieved UP");
                Scene scene = new Scene(createContent());
                primaryStage.setScene(scene);
                primaryStage.show();
            } else if(key.equals(KeyCode.DOWN)){
                System.out.println("recieved DOWN");
            }
        }
    }); 
}

So here I open my window initialised with firstContent (basically it creates an array of empty tiles, and fills two of them with 2 or 4 randomly), display it and start listening for key presses. The idea is to have a behavior for each arrow key (UP DOWN LEFT RIGHT) which will move the tiles accordingly. This is done by the following createContent() method :

    public Parent createContent(){
    String c  = "";
    List<Integer> known = new ArrayList<Integer>();
    Pane root = new Pane();
    root.setPrefSize(740, 700);
    Random rand = new Random();
    int pos1 = rand.nextInt(15);
    if(tiles.get(pos1) != new Tile("")){
        known.add(pos1);
        pos1 = rand.nextInt(15);
        if(known.contains(pos1)){
            known.add(pos1);
            pos1 = rand.nextInt(15);
        }
    }
    for(int i = 0; i < NB_TILES; i++){
        tiles.add(new Tile(c));
    }
    tiles.set(pos1, new Tile("2048"));

    for(int i = 0; i < tiles.size(); i++){
        // boring stuff to set the tile display to the right size
    }
    return root;
}

Now for the problem : when the application is running, if I press the down arrow, I do get on my terminal the "recieved DOWN" text as many times as I press the key as expected. But if I press the up arrow, the application will only recieve it once and the application seems to be frozen (meaning if I press down again, nothing happens). As you could guess, I want to be able to call my method for each key press to be able to move my tiles around and utltimately combine them to get a playable version of 2048... Anyone know why my app gets frozen ?

If needed, I can provide other bits of code but I think I provided the essential. Just know that firstContent() works basically the same as createContent for now except it genereates two random numbers to get the first tiles of the game.

Thanks in advance for your help.

Bruh
  • 147
  • 6
  • 2
    Does it really freeze or does the fact that the app is no longer responding to key presses just make you assume the app is frozen? You're replacing your old scene with a new one that doesn't have a `onKeyPressed` handler registered... – fabian Jan 03 '19 at 13:25
  • @fabian Well, I assume its frozen yes. You mean here ? Scene scene = new Scene(createContent()); thats right, I didnt think about that... how can I keep using the first scene then ? would scene = createContent() be the way to go here ? After some research, I can't figure how to modify a scene... Does it mean that every Scene I create (basically, each time the player want to move something) will have to get its own handler ? That sounds like a huge mess... – Bruh Jan 03 '19 at 14:40
  • 1
    [here](https://github.com/jperedadnr/Game2048HOL) is a great tutorial by @José Pereda. – SedJ601 Jan 03 '19 at 16:55
  • @Sedrick Well although it indeed looks like an awesome tutorial, I'd prefer doing it all myself, especially because it is a school project lol – Bruh Jan 03 '19 at 17:36
  • 1
    @Bruh It is a huge mess. You should instead update the content you intend to move instead. Obviously `createContent` is doing something that you want, consider having it return a container pane of some sort and setting the root of the scene it. Be aware this is just ONE way to do it. – Hypnic Jerk Jan 03 '19 at 21:06
  • @HypnicJerk And how can you update it instead ? just say scene = createContent() ? I'll consider that thanks ! – Bruh Jan 04 '19 at 11:12

0 Answers0