Javafx 如何通过 tilePane 移动对象
Javafx How move objects through tilePane
我有一个 tilePanes 网格,其中对象(动物)作为图像随机放置在上面。在它们真正移动之前,我需要找到一种方法来检查那个特定单元格旁边的四个 slots/cells(北、南、东、西),看看里面是否有食物来源,如果是的话,移动到那个细胞。如果错误尝试下一个方向,或者如果全部错误,则随机移动。
目前它们只是随机移动,如果幸运地在牢房上有食物来源,它们就会进食。这就是我目前拥有的,确实有效
private void makeAnimalsMove() {
Random random = new Random();
// Mark all animals that they haven't moved yet
for (Slot[] row : slots) {
for (Slot slot : row) {
for (Animal animal : slot.getAnimals()) {
animal.setMoved(false);
}
}
}
// Now we move only those who needs to be moved
for (int row = 0; row < slots.length; row++) {
for (int column = 0; column < slots[row].length; column++) {
final Slot slot = slots[row][column];
for (final Animal animal : slot.getAnimals()) {
if (animal.hasMoved()) {
continue;
}
int[][] directions = {
{row - 1, column}, // north
{row, column + 1}, // east
{row + 1, column}, // south
{row, column - 1}, // west
};
int[] selectedDirection = directions[random.nextInt(directions.length)];
// Move the animal to the chosen direction if possible
final int rowDirection = selectedDirection[0];
final int columnDirection = selectedDirection[1];
if (rowDirection >= 0 && rowDirection < slots.length && columnDirection >= 0 && columnDirection < slots[rowDirection].length) {
Platform.runLater(new Runnable() {
@Override
public void run() {
slot.removeObject(animal);
slots[rowDirection][columnDirection].addObject(animal);
}
});
}
// Decrease the animal's life
animal.setMoved(true);
animal.setLifeSpan(animal.getLifeSpan() - 1);
}
}
}
}
'eating' 部分有一个单独的方法,如果单元格包含食物源,将调用该方法。我只是不确定如何让它在移动前检查四个单元格?
为了让我的解决方案生效,我建议您从 TilePane
切换到 GridPane
。
您可以轻松绘制元素网格:
GridPane grid = new GridPane();
grid.add(child, columnIndex, rowIndex);
然后检测特定单元格中是否有东西,例如使用这个助手:
private static boolean isCellOccupied(GridPane gridPane, int column, int row)
{
return gridPane
.getChildren()
.stream()
.filter(Node::isManaged)
.anyMatch(
n -> Objects.equals(GridPane.getRowIndex(n), row)
&& Objects.equals(GridPane.getColumnIndex(n),
column));
}
我有一个 tilePanes 网格,其中对象(动物)作为图像随机放置在上面。在它们真正移动之前,我需要找到一种方法来检查那个特定单元格旁边的四个 slots/cells(北、南、东、西),看看里面是否有食物来源,如果是的话,移动到那个细胞。如果错误尝试下一个方向,或者如果全部错误,则随机移动。
目前它们只是随机移动,如果幸运地在牢房上有食物来源,它们就会进食。这就是我目前拥有的,确实有效
private void makeAnimalsMove() {
Random random = new Random();
// Mark all animals that they haven't moved yet
for (Slot[] row : slots) {
for (Slot slot : row) {
for (Animal animal : slot.getAnimals()) {
animal.setMoved(false);
}
}
}
// Now we move only those who needs to be moved
for (int row = 0; row < slots.length; row++) {
for (int column = 0; column < slots[row].length; column++) {
final Slot slot = slots[row][column];
for (final Animal animal : slot.getAnimals()) {
if (animal.hasMoved()) {
continue;
}
int[][] directions = {
{row - 1, column}, // north
{row, column + 1}, // east
{row + 1, column}, // south
{row, column - 1}, // west
};
int[] selectedDirection = directions[random.nextInt(directions.length)];
// Move the animal to the chosen direction if possible
final int rowDirection = selectedDirection[0];
final int columnDirection = selectedDirection[1];
if (rowDirection >= 0 && rowDirection < slots.length && columnDirection >= 0 && columnDirection < slots[rowDirection].length) {
Platform.runLater(new Runnable() {
@Override
public void run() {
slot.removeObject(animal);
slots[rowDirection][columnDirection].addObject(animal);
}
});
}
// Decrease the animal's life
animal.setMoved(true);
animal.setLifeSpan(animal.getLifeSpan() - 1);
}
}
}
}
'eating' 部分有一个单独的方法,如果单元格包含食物源,将调用该方法。我只是不确定如何让它在移动前检查四个单元格?
为了让我的解决方案生效,我建议您从 TilePane
切换到 GridPane
。
您可以轻松绘制元素网格:
GridPane grid = new GridPane();
grid.add(child, columnIndex, rowIndex);
然后检测特定单元格中是否有东西,例如使用这个助手:
private static boolean isCellOccupied(GridPane gridPane, int column, int row)
{
return gridPane
.getChildren()
.stream()
.filter(Node::isManaged)
.anyMatch(
n -> Objects.equals(GridPane.getRowIndex(n), row)
&& Objects.equals(GridPane.getColumnIndex(n),
column));
}