1

I'm making a 2D game that requires a 2D character which will be able to eventually move around objects itself. I've been trying to get my movement method to work, and whilst I think with help I've cracked it, I've noticed that I've only been adding an image to the gridPane and not the character itself. Whilst I'm confident I can just add an image and manipulate that, I would like to add the keeper from 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;

            }
    }
SteveMunroe
  • 21
  • 1
  • 7
  • please reduce your code and check the formatting – hering Feb 23 '17 at 15:13
  • Are the changes i;ve made slightly better? or is it more beneficial for me to put the whole code into one 'code block'? – SteveMunroe Feb 23 '17 at 15:19
  • remove blank lines, all code in one "code block" could be an advantage, but it still seems to be too much for a http://stackoverflow.com/help/mcve – hering Feb 23 '17 at 15:55

0 Answers0