玩家角色未添加到GridPane Javafx
我正在制作一个2D游戏,需要一个2D角色,它最终能够在物体本身周围移动。我一直在尝试让我的移动方法发挥作用,虽然我认为在帮助下我已经破解了它,但我注意到我只是在网格窗格中添加了一个图像,而不是角色本身。虽然我相信我可以添加一个图像并对其进行操作,但我想从玩家角色未添加到GridPane Javafx,java,javafx,2d,gridpane,Java,Javafx,2d,Gridpane,我正在制作一个2D游戏,需要一个2D角色,它最终能够在物体本身周围移动。我一直在尝试让我的移动方法发挥作用,虽然我认为在帮助下我已经破解了它,但我注意到我只是在网格窗格中添加了一个图像,而不是角色本身。虽然我相信我可以添加一个图像并对其进行操作,但我想从createKeeper()添加keeper public类主扩展应用程序{ 公共静态void main(字符串[]args){ 发射(args); } @凌驾 公众假期开始(阶段){ 创作(舞台); } 私人空间创建(舞台舞台){ GridPa
createKeeper()
添加keeper
public类主扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
创作(舞台);
}
私人空间创建(舞台舞台){
GridPane root=新的GridPane();
getColumnConstraints().add(新的ColumnConstraints(100));
文本t1=新文本(250,50,“Sokoban”);
t1.setId(“标题”);
t1.setFont(Font.Font(java.awt.Font.SERIF,25));
Image Image=新图像(getClass().getResourceAsStream(“title.png”);
标签标签1=新标签(“”);
label1.setGraphic(新图像视图(图像));
标签1.setTranslateY(50);
标签1.setTranslateX(225);
文本t3=新文本(260350,“退出”);
t3.setFont(Font.Font(java.awt.Font.SERIF,25));
按钮btn=新按钮();
btn.setText(“开始”);
btn.setFont(Font.Font(java.awt.Font.SERIF,25));
btn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
级别。运行级别1(舞台);
System.out.println(“你好,世界!”);
}
});
窗格按钮窗格=新窗格();
buttonPane.setId(“根”);
btn.setLayoutX(250);
btn.设置布局(250);
buttonPane.getChildren().addAll(标签1、t3、btn);
场景=新场景(buttonPane,560,560);
buttonPane.getStylesheets().addAll(this.getClass().getResource(“application.css”).toExternalForm());
stage.setresizeable(假);
stage.setTitle(“菜单”);
舞台场景(场景);
stage.show();
}
}
公营班级{
私有静态ImageView image1=ImageLoader.showWareHouseImage();
专用静态最终整数键盘_移动_增量=2;
静态int-xPosition;
静态内定位;
静态仓库管理员=新仓库管理员(图像1,xPosition,yPosition);;
公共静态无效运行级别1(后台){
keeper.yPosition=5;
keeper.xPosition=5;
组根=新组();
整列金额=12;
整数行数=12;
GridPane gameGrid=新建GridPane();
对于(int i=0;i{
event.consume();
开关(event.getCode()){
案例W:
System.out.println(“up”);
keeper.setyPosition(keeper.getyPosition()-键盘\移动\增量);
打破
案例D:
keeper.setxPosition(keeper.getxPosition()+键盘+移动+增量);
打破
案例A:
keeper.setyPosition(keeper.getyPosition()+键盘+移动+增量);
打破
案例S:
keeper.setxPosition(keeper.getxPosition()-键盘\u移动\u增量);
打破
}
});
}
}
公共类WarehouseKeeper扩展ImageLoader{
私人影像播放影像;
私有图像查看图像;
私人整数速度;
int-xPosition;
内部位置;
公共仓库管理员(ImageView仓库图像、int-xPosition、int-yPosition){
超级(仓库形象);
速度=速度;
this.xPosition=xPosition;
this.yPosition=yPosition;
}
public int getxPosition(){
返回xPosition;
}
公共void setxPosition(int xPosition){
this.xPosition=xPosition;
}
public int getyPosition(){
返回位置;
}
公共无效设置位置(int-yPosition){
this.yPosition=yPosition;
}
}
公共类图像加载器{
公共静态图像查看仓库图像;
私有对象钻石图像;
普里夫
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;
}
}