From 587e2bf6993efb5db9df9faa3182c3ab2ea47394 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 16 May 2023 16:55:17 +0200 Subject: [PATCH] make button join the level --- .../java/school_project/Menu/MenuLevel.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/school_project/Menu/MenuLevel.java b/app/src/main/java/school_project/Menu/MenuLevel.java index f86f699..af3c67a 100644 --- a/app/src/main/java/school_project/Menu/MenuLevel.java +++ b/app/src/main/java/school_project/Menu/MenuLevel.java @@ -7,6 +7,11 @@ import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.RowConstraints; import school_project.Controller; +import school_project.GameUI; +import school_project.Parsers.FileParserFactory; + +import java.io.File; +import java.io.IOException; public class MenuLevel extends GridPane { private int StartLevel; @@ -60,30 +65,38 @@ public class MenuLevel extends GridPane { //It's here that I put all buttons where I need (base on column not row) for (int i = 0; i < 3; i++) { for (int j = 1; j < 5; j++) { + Button levelButton = new Button("level "+(StartLevel)); + levelButton.setOnAction(event -> { + try { + String levelName = ((Button)event.getSource()).getText().replace(" ", "") + ".level"; + System.out.println(levelName); + GameUI level = new GameUI(FileParserFactory.loadMapFromFile(new File(Controller.class.getResource("levels/" + levelName).getFile()))); + Controller.switchRoot(level); + } catch (IOException e) { + System.out.println("Le niveau " + StartLevel + "n'existe pas."); + } + }); if(i==0){ - Button Level = new Button("level "+(StartLevel)); StartLevel+=3; - add(Level,i,j); - setHalignment(Level,HPos.CENTER); + add(levelButton,i,j); + setHalignment(levelButton,HPos.CENTER); if(j==4){ StartLevel-=11; } } else if(i==1&&j!=4) { - Button Level = new Button("level "+(StartLevel)); StartLevel += 3; - add(Level, i, j); - setHalignment(Level,HPos.CENTER); + add(levelButton, i, j); + setHalignment(levelButton,HPos.CENTER); if (j == 3) { StartLevel -=8; } } else if(i==2&&j!=4){ - Button Level = new Button("level "+(StartLevel)); StartLevel+=3; - add(Level,i,j); - setHalignment(Level,HPos.CENTER); + add(levelButton,i,j); + setHalignment(levelButton,HPos.CENTER); } } }