0

This is kinda the source code. After a lot of tries I managed to get to this point, but I'm still getting a lot of errors.

java.lang.reflect.InvocationTargetException Caused by: java.lang.RuntimeException: Exception in Application start method Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT

I'm not even sure if what I'm trying to achieve here is going to the right direction or if it is completely wrong.

The source code runs the game without any additional screens. I want to add a simple start screen before the game starts. A play button and a dropdown menu. The button sends you to the game and in the dropdown menu you choose a color and it saves it for in-game use.

@Override
public void start(Stage primaryStage)  {

//MENU ITEMS HERE//

    gameSceneGrid = new GridPane();
    GridPane root = new GridPane();
    root.add(MENU, 0, 0);
    root.add(gameSceneGrid, 0, 1);
    root.getChildren().addAll(gameSceneGrid);
    gameScene = new Scene(root,600,600);

    Button playButton = new Button("Play Game");
    playButton.setOnAction(e -> primaryStage.setScene(gameScene));

    startSceneGrid = new GridPane();
    startSceneGrid.add(playButton,0,1);
    GridPane root0= new GridPane();
    root0.add(MENU,0,0);
    root0.add(startSceneGrid,0,1);
    root0.getChildren().addAll(startSceneGrid);
    startScene = new Scene(root0,200,200);



    primaryStage.setScene(startScene);
    primaryStage.setTitle(StartMeUp.GAME_NAME);
    primaryStage.show();
    loadDefaultSaveFile(primaryStage);
}


void loadDefaultSaveFile(Stage primaryStage)
{
    this.primaryStage = primaryStage;
    System.out.println("Hi");
    InputStream in = getClass().getClassLoader().getResourceAsStream("sample/SampleGame.skb");
    System.out.println(in);
    initializeGame(in);
    System.out.println("Hi");
    setEventFilter();
    System.out.println("Hi");
}

This is the original source code for the game screen/scene

        gameGrid = new GridPane();
        GridPane root = new GridPane();
        root.add(MENU, 0, 0);
        root.add(gameGrid, 0, 1);
        primaryStage.setTitle(StartMeUp.GAME_NAME);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
        loadDefaultSaveFile(primaryStage);
Gabriel
  • 9
  • 2
  • 1
    Post the complete stack trace and indicate which line in the code is throwing the exception – James_D Nov 02 '20 at 20:38
  • 3
    That said, you're adding `gameSceneGrid` to `root` twice, which is almost certainly the cause of the problem. – James_D Nov 02 '20 at 20:39
  • [mcve] please.. – kleopatra Nov 02 '20 at 21:07
  • I deleted root.getChildren.adAll and it worked. I could never have imagined that I was so close. Thanks so much. @James_D – Gabriel Nov 02 '20 at 21:21
  • 3
    The important part of the stack trace was `Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT`, which tells you you're adding a `Node` to a `Parent` more than once simultaneously. The stack trace will also tell what line of your code threw the error. If you're not sure how to read a stack trace then check out [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/). – Slaw Nov 03 '20 at 02:40

0 Answers0