2017-03-12 57 views
1

已將我的代碼更改爲此。嘗試從我的Game類實例化新的Level 1。然而,現在沒有什麼顯示當我運行應用程序。在我的遊戲中添加第二個關卡

public class Game extends Application { 

    Pane backgroundPane; 
    Pane playfieldLayer; 
    Pane scoreLayer; 

    Level2 level2; 

    Image playerImage = new Image(getClass().getResource("warehouse.png").toExternalForm()); 
    Image wallImage = new Image(getClass().getResource("Wall.png").toExternalForm()); 
    Image foodImage = new Image(getClass().getResource("food.png").toExternalForm()); 
    Image diamondImage = new Image(getClass().getResource("Chef.png").toExternalForm()); 

    Player player; 
    Wall wall; 

    List<Player> players = new ArrayList<>(); 
    List<Food> foods = new ArrayList<>(); 
    List<Wall> walls = new ArrayList<>(); 
    List<Diamonds> diamonds = new ArrayList<>(); 

    boolean collision; 
    boolean wallCollision; 
    boolean foodWallCollision; 
    boolean won = false; 

    GridPane gameGrid; 
    static Scene scene; 
    Stage theStage; 

    Input input; 




    @Override 

    public void start(Stage theStage) { 


     Menu menu = new Menu(); 
     Scene menuView = new Scene(menu, Settings.SCENE_HEIGHT, Settings.SCENE_WIDTH); 

     theStage.setScene(menuView); 
     // theStage.setResizable(false); 

     theStage.show(); 

     Menu.start.setOnAction(new EventHandler<ActionEvent>() { 
      public void handle(ActionEvent event) { 

       runGame(theStage); 

      } 
     }); 

    } 

    public void runGame(Stage theStage) { 

     // Input input = new Input(scene); 

     Group root = new Group(); 

     GridPane gameGrid = new GamePane(14, 14); 
     gameGrid.setStyle("-fx-background-color: white; -fx-grid-lines-visible:true"); 
     backgroundPane = new Pane(); 
     backgroundPane.setId("root"); 
     playfieldLayer = new Pane(); 
     scoreLayer = new Pane(); 

     playfieldLayer.getChildren().add(gameGrid); 
     root.getChildren().add(backgroundPane); 
     root.getChildren().add(playfieldLayer); 

     // scene = new Scene(root, (columnAmount * 40) + 66, (rowAmount * 40) + 
     // 66, Color.WHITE); 

     scene = new Scene(root, 600, 500); 
     backgroundPane.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm()); 

     // theStage.setResizable(false); 
     theStage.setScene(scene); 
     theStage.show(); 
     level2 = new Level2(playerImage, wallImage, foodImage, diamondImage, player, wall, gameGrid, theStage, input, collision, wallCollision, foodWallCollision, players, foods, walls, diamonds); 
     level2.startLevel2(); 
    } 

    public static void main(String[] args) { 
     launch(args); 


    } 

} 

等級1級(我會分裂的代碼還我只是試圖讓事情到目前運行)

public abstract class Level1 { 


    Pane backgroundPane; 
    Pane playfieldLayer; 
    Pane scoreLayer; 

    Image playerImage; 
     Image wallImage; 
     Image foodImage; 
    Image diamondImage; 

    Player player; 
     Wall wall; 

    List<Player> players = new ArrayList<>(); 
    List<Food> foods = new ArrayList<>(); 
    List<Wall> walls = new ArrayList<>(); 
    List<Diamonds> diamonds = new ArrayList<>(); 

    boolean collision; 
    boolean wallCollision; 
    boolean foodWallCollision; 
    boolean won = false; 

    GridPane gameGrid; 
    Scene scene; 
    Stage theStage; 

    Input input; 


     public Level1(Image playerImage, Image wallImage, 
       Image foodImage, Image diamondImage, Player player, Wall wall, GridPane gameGrid, Stage theStage, 
       Input input, boolean collision, boolean wallCollision, boolean foodWallCollision, List<Player> players, 
       List<Food> foods,List<Wall> walls, List<Diamonds> diamonds) 
     { 



      this.playerImage = playerImage; 
      this.wallImage = wallImage; 
      this.foodImage = foodImage; 
      this.diamondImage = diamondImage; 
      this.player = player; 
      this.wall = wall; 
      this.gameGrid = gameGrid; 
      this.theStage = theStage; 
      this.input = input; 
      this.collision = collision; 
      this.wallCollision = wallCollision; 
      this.foodWallCollision = foodWallCollision; 
      this.players = players; 
      this.foods = foods; 
      this.walls = walls; 
      this.diamonds = diamonds; 

      //a billion getters and setters below. 


     } 

這是我的level2的類,我想成爲第一級顯示。我的try catch語句在程序運行時被捕獲,所以這與我如何將項目產生到我的遊戲領域上有關。調用Application#launch()(見the life cycle of a JavaFX Application)時

public class Level2 extends Level1{ 

    public Level2(Image playerImage, Image wallImage, 
      Image foodImage, Image diamondImage, Player player, Wall wall, GridPane gameGrid, Stage theStage, 
      Input input, boolean collision, boolean wallCollision, boolean foodWallCollision, List<Player> players, 
      List<Food> foods,List<Wall> walls, List<Diamonds> diamonds){ 

     super(playerImage, wallImage, foodImage,diamondImage, player, 
       wall, gameGrid, theStage, input, collision, wallCollision, foodWallCollision, players, foods, walls, diamonds); 
     // TODO Auto-generated constructor stub 
    } 

    public void startLevel2(){ 



     try { 

      createPlayers(); 
      spawnFood(); 
      spawnWall(); 
      spawnDiamonds(); 

     } catch (NullPointerException e) { 

      System.out.println("You are missing the pictures for the spawn methods"); 
     } 



     AnimationTimer gameLoop = new AnimationTimer() { 

      @Override 
      public void handle(long now) { 

       // player input 
       players.forEach(sprite -> sprite.processInput()); 
       foods.forEach(sprite -> sprite.processInput()); 

       // movement 
       players.forEach(sprite -> sprite.move()); 
       foods.forEach(sprite -> sprite.move()); 
       // ((Player) players).stopInput(); 



       // check collisions 


       // update sprites in scene 
       players.forEach(sprite -> sprite.updateUI()); 
       foods.forEach(sprite -> sprite.updateUI()); 

      } 

     }; 
     gameLoop.start(); 

    } 



    private void createPlayers() { 

     Input input = new Input(scene); 

     input.addListeners(); 

     Image image = playerImage; 

     double x = (Settings.SCENE_WIDTH - image.getWidth())/1.0; 
     double y = Settings.SCENE_HEIGHT * 0.8; 

     Player player = new Player(playfieldLayer, image, x, y, 0, 0, 0, input); 

     players.add(player); 

    } 

    private void spawnFood() { 

     Input input = new Input(scene); 

     input.addListeners(); 

     Image image = foodImage; 

     double x = (Settings.SCENE_WIDTH - image.getWidth())/2.0; 
     double y = Settings.SCENE_HEIGHT * 0.4; 


     //Food food1 = new Food(playfieldLayer, image, 150, 50, 0,0); 

     Food food = new Food(playfieldLayer, image, x, y, 0, 0,input); 

     foods.add(food); 
     //foods.add(food1); 

    } 



} 
+0

不知道,因爲我覺得這種代碼方式太複雜,無法理解。你的ctor只在田地裏很好,但是我們會說,這個班裏的田地數量大約是10或15個條目太長。如此多的領域意味着你在這個貧窮階層中做了太多事情。良好的面向對象的本質是將不同的職責分離到不同的類別中! – GhostCat

+0

我很尷尬的這段代碼。把我的碰撞方法分成他們自己的碰撞類會更有意義嗎? – SteveMunroe

+0

可能。但細節將需要看你的整個代碼庫。我的建議:閱讀關於單一責任原則。然後創建一個新的PER級責任。並尋找一些有經驗的人來審查你的代碼庫。最後:不要感到尷尬。學習編程的真正美德只需要不斷的練習。 – GhostCat

回答

0

JavaFX應用程序類不能帶參數的構造函數,如創建一個實例。如果您需要您當前的構造函數,請確保您也有一個無參數的構造函數。

但是,將應用程序邏輯和級別邏輯分開可能是一個好主意,因爲在將來添加更多級別時,它可能會變得退出混亂。

+0

謝謝你的評論。我試過進一步分解我的代碼(如上所示),但現在不顯示任何內容。 – SteveMunroe

+0

您分解了代碼,解決了您在第一個過程中描述的問題。所以當其他問題開始出現時,請考慮創建一個新問題,或者努力自己解決問題。現在你的新問題已經超出了這個問題的範圍,並且以廣泛的方式回答,並以一種好的方式回答。 – n247s

+0

好吧,謝謝你的建議,我試着投票你的答案,但它似乎並沒有太大的作用。我會做一個新的問題:) – SteveMunroe