我正在使用Javafx嘗試創建一個2D玩家,玩家(玩家等級)將能夠將一個箱子(食品類)移動到某個位置。我可以讓玩家與我想移動的圖像發生碰撞,但不會在網格上移動。我已經設置了一個system.out消息來充當一個調試器,但仍然沒有動作。對象不會碰撞
public class Game extends Application{
Pane backgroundPane;
Pane playfieldLayer;
Pane scoreLayer;
Image playerImage;
Image foodImage;
List<Player> players = new ArrayList<>();
List<Food> foods = new ArrayList<>();
Text collisionText = new Text();
boolean collision = false;
Scene scene;
@Override
public void start(Stage theStage) {
Group root = new Group();
int columnAmount = 18;
int rowAmount = 18;
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");
root.getChildren().add(gameGrid);
// create layers
backgroundPane = new Pane();
backgroundPane.setId("root");
playfieldLayer = new Pane();
scoreLayer = new Pane();
root.getChildren().add(backgroundPane);
root.getChildren().add(playfieldLayer);
root.getChildren().add(scoreLayer);
scene = new Scene(root, Settings.SCENE_WIDTH, Settings.SCENE_HEIGHT);
backgroundPane.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());
theStage.setResizable(false);
theStage.setScene(scene);
theStage.show();
loadGame();
createPlayers();
AnimationTimer gameLoop = new AnimationTimer() {
@Override
public void handle(long now) {
// player input
players.forEach(sprite -> sprite.processInput());
spawnFood();
players.forEach(sprite -> sprite.move());
foods.forEach(sprite -> sprite.move());
checkCollisions();
players.forEach(sprite -> sprite.updateUI());
foods.forEach(sprite -> sprite.updateUI());
}
};
gameLoop.start();
}
private void loadGame() {
playerImage = new Image(getClass().getResource("warehouse.png").toExternalForm());
//enemyImage = new Image(getClass().getResource("enemy.png").toExternalForm());
foodImage = new Image(getClass().getResource("food.png").toExternalForm());
}
private void createPlayers() {
// player input
Input input = new Input(scene);
// register input listeners
input.addListeners(); // TODO: remove listeners on game over
Image image = playerImage;
// center horizontally, position at 70% vertically
double x = (Settings.SCENE_WIDTH - image.getWidth())/2.0;
double y = Settings.SCENE_HEIGHT * 0.7;
// create player
Player player = new Player(playfieldLayer, image, x, y, 0, 0, 0, Settings.PLAYER_SPEED, input);
// register player
players.add(player);
}
Input input = new Input(scene);
input.addListeners();
Image image = foodImage;
double x = (Settings.SCENE_WIDTH - image.getWidth())/2.0;
double y = Settings.SCENE_HEIGHT * 0.2;
Food food = new Food(playfieldLayer, image, x,y,0,0, Settings.PLAYER_SHIP_SPEED, input);
foods.add(food);
private void checkCollisions() {
collision = false;
for(Player player: players) {
for(Food food: foods) {
if(player.collidesWith(food)) {
Food.collision = true;
food.updateUI();
System.out.println("Collided");
}
}
}
}
public static void main(String[] args) {
launch(args);
}
}
public abstract class SpriteBase {
public void move() {
if(!canMove)
return;
x += dx;
y += dy;
}
public void updateUI() {
imageView.relocate(x, y);
}
}
public class Food extends SpriteBase {
double foodMinX;
double foodMaxX;
double foodMinY;
double foodMaxY;
double speed;
Input input;
static boolean collision;
public Food(Pane layer, Image image, double x, double y, double dx, double dy, double speed, Input input) {
super(layer, image, x, y, dx, dy);
this.speed = speed;
this.input = input;
checkBounds1();
}
private void checkBounds1() {
// calculate movement bounds of the player ship
// allow half of the ship to be outside of the screen
foodMinX = 0 - image.getWidth()/2.0;
foodMaxX = Settings.SCENE_WIDTH - image.getWidth()/2.0;
foodMinY = 0 - image.getHeight()/2.0;
foodMaxY = Settings.SCENE_HEIGHT -image.getHeight()/2.0;
}
public void processInput() {
if(input.isMoveUp() && collision != false) {
input.removeListeners();
dy = -speed;
collision = false;
System.out.println("move up you bastard");
} else if(input.isMoveDown() && collision != false) {
input.removeListeners();
System.out.println("move up you bastard");
dy = speed;
collision = false;
} else {
dy = 0d;
}
// horizontal direction
if(input.isMoveLeft() && collision == true) {
input.removeListeners();
System.out.println("move up you bastard");
collision = false;
dx = -speed;
} else if(input.isMoveRight() && collision == true) {
input.removeListeners();
System.out.println("move up you bastard");
collision = false;
dx = speed;
} else {
dx = 0d;
}
}
@Override
public void move() {
super.move();
// ensure the food can't move outside of the screen
checkBounds();
}
private void checkBounds() {
// vertical
if(Double.compare(y, foodMinY) < 0) {
y = foodMinY;
} else if(Double.compare(y, foodMaxY) > 0) {
y = foodMaxY;
}
// horizontal
if(Double.compare(x, foodMinX) < 0) {
x = foodMinX;
} else if(Double.compare(x, foodMaxX) > 0) {
x = foodMaxX;
}
}
我懷疑你發佈的代碼比大多數人想要瀏覽的要多得多。嘗試並將其減少到[mcve] – khelwood
設置食物時,您將'0'傳遞給'dx'和'dy'。所以'move()'函數被調用,它只是給你的'x'和'y'加'0'。在你的'spawnFoods'方法中。 –
我減少了一些。你認爲它看起來更好嗎?還是你會建議進一步削減它? 謝謝你的回覆Hypnic Jerk,非常友善。你會建議如何解決這個問題? – callumSteven