JavaFx移动图像

时间:2017-02-16 00:20:21

标签: java javafx sprite game-physics

大家好,所以我正在创建一个简单的2D游戏,到目前为止,我已经做了一些噩梦,试图让事情相互沟通。到目前为止,我有一个“主菜单”,“游戏网格”和一个显示在网格上的图像。我现在正在解决这个问题,但我似乎无法让它发挥作用。

bind'

}

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;

}

}

1 个答案:

答案 0 :(得分:0)

问题可能在于您理解对象如何定向JavaFX,该对象可能由WareHouseKeeper和ImageLoader类创建,但该对象在级别类上实例化,这意味着如果您在级别类上实例化该对象它必须在级别类中移动。例如,你拥有的是

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

由于对象已传递给函数,您只需要从级别类中直接移动它而不是从其他类移动它,因此这将更改为

    private static void moveWareHouse(Stage theStage, Group wHK) {

        theStage.addEventFilter(KeyEvent.KEY_RELEASED, e -> {
        e.consume();

        switch (e.getCharacter()) {

        case W:
            keeper.setLayoutY(keeper.getLayoutY() - KEYBOARD_MOVEMENT_DELTA);
            break;
        case D:
            keeper.setLayoutX(keeper.getLayoutX() + KEYBOARD_MOVEMENT_DELTA);
            break;
        case A:
            keeper.setLayoutY(keeper.getLayoutY() + KEYBOARD_MOVEMENT_DELTA);
            break;
        case S:
            keeper.setLayoutX(keeper.getLayoutX() - KEYBOARD_MOVEMENT_DELTA);
            break;
        }
    });
}

修改 我还注意到你将场景解析为关键监听器,如果你将舞台设置为关键监听器,无论你在哪里点击它,都需要你点击场景(给它焦点)。将永远是根本焦点,并始终倾听关键。

再次编辑 正如我在上面的对话中提到的那样,最好在WarehouseKeeper类中创建整个WarehouseKeeper并将其实例化到主类(level1)中,这样对象的控制在level1内,但level1并不是全部用于创建它的代码(因此,如果您需要稍后修改它的创建或再次使用它,您可以通过创建它的新实例并将其添加到场景中来调用更多它们),例如。

public final Group warehouseKeeper(ImageView image) {
    Group tempGroup;
    /* insert necessary code here to add image to group and anything else you want in the group */
    return tempGroup;
}

然后你可以通过调用

将它实例化为level1
final Group wHK = new WarehouseKeeper.warehouseKeeper();
wHK.setLayoutX(/*some init val */);
wHK.setLayoutY(/*some init val */);
root.getChildren().add(wHK);