Ich programmiere ein Spiel in Java. Es ist wie ein Labyrinth. Klickt ein Spieler auf eine Zelle auf einer Spielfläche, bewegt sich das Bild des Spielers dorthin und ich muss mit dem Entfernen des playerImage von der alten Position kämpfen.ImageView von Gridpane entfernen
Hier ist, wie ich die imageViews erschaffe:
private ImageView[][] initImages() {
int colcount = gridPane.getColumnConstraints().size();
int rowcount = gridPane.getRowConstraints().size();
imageViews = new ImageView[colcount][rowcount];
// bind each Imageview to a cell of the gridpane
int cellWidth = (int) gridPane.getWidth()/colcount;
int cellHeight = (int) gridPane.getHeight()/rowcount;
for (int x = 0; x < colcount; x++) {
for (int y = 0; y < rowcount; y++) {
//creates an empty imageview
imageViews[x][y] = new ImageView();
//image has to fit a cell and mustn't preserve ratio
imageViews[x][y].setFitWidth(cellWidth);
imageViews[x][y].setFitHeight(cellHeight);
imageViews[x][y].setPreserveRatio(false);
imageViews[x][y].setSmooth(true);
//add the imageview to the cell and
//assign the correct indicees for this imageview, so you later can use GridPane.getColumnIndex(Node)
gridPane.add(imageViews[x][y], x, y);
//the image shall resize when the cell resizes
imageViews[x][y].fitWidthProperty().bind(gridPane.widthProperty().divide(colcount));
imageViews[x][y].fitHeightProperty().bind(gridPane.heightProperty().divide(rowcount));
}
}
und hier ist, wie ich einen Spieler auf einem
public RealGUI drawPlayer(int playerNumber, CellCoordinates coords) {
Image img = null;
switch (playerNumber) {
case 1:
img = new Image("/gui/img/Bob_trans.png");
break;
case 2:
// trans Bild Spieler 2
img = new Image("/gui/img/Bob_trans.png");
break;
default:
System.out.print("Problem mit Spielernummer");
}
ImageView newImgView = new ImageView();
newImgView.fitHeightProperty().bind(this.gridPane.heightProperty()
.divide(this.gridPane.getRowConstraints().size()));
newImgView.setImage(img);
this.gridPane.add(newImgView, coords.getX(), coords.getY());
return this;
}
Meine grundlegende Frage
OnClickEvent ziehen wird: Wie entferne ich den Player Bildansicht auf dem GridPane?Vielen Dank, Daniel