大家好,我正在创建一个简单的2D游戏,到目前为止,我已经有一些噩梦试图让事情互相沟通。到目前为止,我有一个'主菜单','游戏网格'和一个显示在网格上的图像。我现在正在解决它的运动,但我似乎无法得到它的工作。JavaFx移动图像
package application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.Group;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.image.Image;
public class Level {
// private static Array[] level1;
// private WarehouseKeeper warehouse;
private static ImageView image1 = ImageLoader.showWareHouseImage();
private static final int KEYBOARD_MOVEMENT_DELTA = 2;
public static void runLevel1(Stage theStage) {
// ImageView image = new ImageView((Element)
// ImageLoader.wareHouseImage);
// WarehouseKeeper warehouse = new
// WarehouseKeeper(ImageLoader.wareHouseImage);
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);
image(root, gameGrid);
moveWareHouse(scene, createKeeper());
theStage.setScene(scene);
theStage.show();
}
private static void image(Group root, GridPane gameGrid) {
// ImageLoader.wareHouseImage;
/*
* ImageView wareHouse = new ImageView(); wareHouse.setFitHeight(45);
* wareHouse.setFitWidth(45);
*
* Image image1 = ImageLoader.showWareHouseImage();
* wareHouse.setImage(image1);
*/
ImageView image1 = ImageLoader.showWareHouseImage();
final HBox picture = new HBox();
picture.getChildren().add(image1);
gameGrid.setAlignment(Pos.CENTER);
// gameGrid.getChildren().add(wareHouse);
gameGrid.add(picture, 7, 9);
root.getChildren().add(gameGrid);
}
private static WarehouseKeeper createKeeper() {
final WarehouseKeeper keeper = new WarehouseKeeper(image1);
return keeper;
}
private static void moveWareHouse(Scene scene, final WarehouseKeeper keeper) {
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
switch (event.getCode()) {
case W:
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;
}
}
});
}
}
package application;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
public class WarehouseKeeper extends ImageLoader {
private Image playerImage;
private ImageView image;
private int speed;
private double xPosition;
private double yPosition;
public WarehouseKeeper(ImageView wareHouseImage) {
super(wareHouseImage);
this.speed = speed;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.wareHouseImage.relocate(xPosition, yPosition);
}
public void updateUI() {
wareHouseImage.relocate(xPosition, yPosition);
}
public double getCenterX() {
return xPosition * 0.5;
}
public double getCenterY() {
return yPosition * 0.5;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public double getxPosition() {
return xPosition;
}
// one of the ideas i had
public void setxPosition(double xPosition) {
this.xPosition = xPosition.setTranslateX(1.0);
}
public double getyPosition() {
return yPosition;
}
public void setyPosition(double d) {
this.yPosition = d + 1.0;
}
public void setTranslateY(double yPosition) {
// TODO Auto-generated method stub
yPosition = yPosition + 2.0;
}
}
package application;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ImageLoader {
protected 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 void setWareHouseImage(Image wareHouseImage) {
this.wareHouseImage = showWareHouseImage();
}
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;
}
public Object getDiamondImage() {
return diamondImage;
}
public void setDiamondImage(Object diamondImage) {
this.diamondImage = diamondImage;
}
public Object getWallImage() {
return wallImage;
}
public void setWallImage(Object wallImage) {
this.wallImage = wallImage;
}
public Object getMapImage() {
return mapImage;
}
public void setMapImage(Object mapImage) {
this.mapImage = mapImage;
}
public Object getCrateImage() {
return crateImage;
}
public void setCrateImage(Object crateImage) {
this.crateImage = crateImage;
}
}
它instantize到1级,当你打电话给你使用的是'你的键盘命令object.xPosition'你试图使用'object.setLayoutX = value',也只需要注意,你已经使用函数来尝试返回一个你可以得到的值,而不需要把它放在一个函数中,例如'case W: keeper.setyPosition(keeper.getyPosition()--KEYBOARD_MOVEMENT_DELTA); break;' 可能只是'case W: keeper.setyPosition(keeper.getLayoutY() - KEYBOARD_MOVEMENT_DELTA); break;'不需要功能。 – TravisF
谢谢你们两位的回复,非常友善。 你会推荐我在哪里放置object.setLayoutX = value方法? – SteveMunroe
您需要将它们放到您的级别类中,因为这是WarehouseKeeper对象被实例化的地方(当它被赋予一个地方并且它需要成为一个节点时),可以将该命令发送到另一个类,但是为了保持它的整洁,你只需在与之类似的类中进行操作,如果你阅读下面的答案,那么你的答案大部分都是正确的,你只需要改变你的键盘记录器就可以使用你的关卡类中的实例化对象。 – TravisF