在我的游戏中添加第二级

时间:2017-03-12 20:16:32

标签: java javafx constructor

将我的代码更改为此。试图从我的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语句,这与我如何将项目生成到我的游戏区域有关。

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);

    }



}

1 个答案:

答案 0 :(得分:0)

JavaFX Application类不能包含带参数的构造函数,因为在调用Application#launch()时会创建一个实例(请参阅the life cycle of a JavaFX Application)。如果您需要当前的构造函数,请确保您还有一个无参构造函数。

但也许将应用程序逻辑和级别逻辑分开是一个好主意,因为在将来添加更多级别时它可能会变得混乱。