玩家角色没有添加到GridPane Javafx

时间:2017-02-23 13:28:02

标签: java javafx 2d gridpane

我正在制作一个2D游戏,需要一个2D角色才能最终在物体周围移动。我一直试图让我的运动方法起作用,虽然我想帮助我破解它,但我注意到我只是在gridPane中添加了一个图像而不是角色本身。虽然我有信心我可以添加图像并操纵它,但我想从createKeeper()添加守护者。

     public class Main extends Application {

            public static void main(String[] args) {

            launch(args);
        }

            @Override

            public void start(Stage stage) {

       create(stage);

        }

            private void create(Stage theStage) {

                GridPane root = new GridPane();
                root.getColumnConstraints().add(new ColumnConstraints(100));

                Text t1 = new Text(250, 50, "Sokoban");
                t1.setId("title");
                t1.setFont(Font.font(java.awt.Font.SERIF, 25));

                Image image = new Image(getClass().getResourceAsStream("title.png"));
                Label label1 = new Label("");
                label1.setGraphic(new ImageView(image));
                label1.setTranslateY(50);
                label1.setTranslateX(225);

                Text t3 = new Text(260, 350, "Quit");
                t3.setFont(Font.font(java.awt.Font.SERIF, 25));

                Button btn = new Button();

                btn.setText("Start");
                btn.setFont(Font.font(java.awt.Font.SERIF, 25));

                btn.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {

                        Level.runLevel1(theStage);

                        System.out.println("Hello World!");
                    }
                });

                Pane buttonPane = new Pane();
                buttonPane.setId("root");

                btn.setLayoutX(250);
                btn.setLayoutY(250);

                buttonPane.getChildren().addAll(label1, t3, btn);

                Scene scene = new Scene(buttonPane, 560, 560);
                buttonPane.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());

                theStage.setResizable(false);
                theStage.setTitle("Menu");
                theStage.setScene(scene);
                theStage.show();


            }               


   }


        public class Level {

private static ImageView image1 = ImageLoader.showWareHouseImage();
            private static final int KEYBOARD_MOVEMENT_DELTA = 2;
            static int xPosition;
            static int yPosition;
            static WarehouseKeeper keeper = new WarehouseKeeper(image1, xPosition, yPosition);;

            public static void runLevel1(Stage theStage) {

                keeper.yPosition = 5;
                keeper.xPosition = 5;



                Group root = new Group();

                int columnAmount = 12;
                int rowAmount = 12;

                GridPane gameGrid = new GridPane();

                for (int i = 0; i < columnAmount; i++) {
                    ColumnConstraints columnn = new ColumnConstraints(45);
                    gameGrid.getColumnConstraints().add(columnn);

                }

                for (int i = 0; i < rowAmount; i++) {

                    RowConstraints row = new RowConstraints(45);
                    gameGrid.getRowConstraints().add(row);
                }

                gameGrid.setStyle("-fx-background-color: white; -fx-grid-lines-visible:true");
                Scene scene = new Scene(root, (columnAmount * 40) + 66, (rowAmount * 40) + 66, Color.WHITE);

                root.getChildren().add(gameGrid);

                gameGrid.add(image1, xPosition, yPosition);

                moveWareHouse(theStage, keeper);

                theStage.setScene(scene);

                theStage.show();

            }



            private static void moveWareHouse(Stage theStage, WarehouseKeeper keeper) {

                theStage.addEventFilter(KeyEvent.KEY_RELEASED, event -> {

                    event.consume();

                    switch (event.getCode()) {

                    case W:
                        System.out.println("up");
                        keeper.setyPosition(keeper.getyPosition() - KEYBOARD_MOVEMENT_DELTA);
                        break;
                    case D:
                        keeper.setxPosition(keeper.getxPosition() + KEYBOARD_MOVEMENT_DELTA);
                        break;
                    case A:
                        keeper.setyPosition(keeper.getyPosition() + KEYBOARD_MOVEMENT_DELTA);
                        break;
                    case S:
                        keeper.setxPosition(keeper.getxPosition() - KEYBOARD_MOVEMENT_DELTA);
                        break;

                    }
                });

            }

  }


        public class WarehouseKeeper extends ImageLoader {

            private Image playerImage;
            private ImageView image;
            private int speed;
            int xPosition;
            int yPosition;

            public WarehouseKeeper(ImageView wareHouseImage, int xPosition, int yPosition) {

                super(wareHouseImage);

                this.speed = speed;
                this.xPosition = xPosition;
                this.yPosition = yPosition;

            }

            public int getxPosition() {
                return xPosition;
            }

            public void setxPosition(int xPosition) {
                this.xPosition = xPosition;
            }

            public int getyPosition() {
                return yPosition;
            }

            public void setyPosition(int yPosition) {
                this.yPosition = yPosition;
            }

   }



      public class ImageLoader {


            public static ImageView wareHouseImage;
            private Object diamondImage;
            private Object wallImage;
            private Object mapImage;
            private Object crateImage;


            public ImageLoader(ImageView wareHouseImage){

                this.wareHouseImage = showWareHouseImage();


            }



            public  ImageView getWareHouseImage() {
                return wareHouseImage;
            }



            public Image setWareHouseImage(Image wareHouseImage) {
                this.wareHouseImage = showWareHouseImage();

                return wareHouseImage;
            }



            public static ImageView showWareHouseImage() {

                ImageView wareHouse = new ImageView();
                wareHouse.setFitHeight(45);
                wareHouse.setFitWidth(45);

                Image wareHouseImage = new Image("application/warehouse.png");
                wareHouse.setImage(wareHouseImage);



                return wareHouse;

            }
    }

0 个答案:

没有答案