私は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
になります。食べ物をセットアップするとき、あなたは 'dx'と' dy'に '0'を渡しています。 'move()'関数が呼び出されると、 'x'と' y'に '0'が追加されます。あなたの 'spawnFoods'メソッドで。 –
私は幾分それを減らしました。あなたはそれがより良く見えると思いますか、それをさらに減らすことを提案しますか? レスポンスありがとうございますHypnic Jerk、非常に親切です。あなたはそれを修正するために何を提案しますか? – callumSteven