Javafx如何通过tilePane移动对象 [英] Javafx How move objects through tilePane

查看:161
本文介绍了Javafx如何通过tilePane移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tilePanes网格,其中对象(动物)随机放置在它上面,作为图像。在它们实际移动之前,我需要找到一种方法来检查该特定单元格(北,南,东,西)旁边的四个槽/单元格,以查看其中是否有食物来源,如果为真,请移至细胞。如果为false则尝试下一个方向,或者如果全部为假,则只需随机移动。

I have a grid of tilePanes, where objects (Animals) are placed randomly onto it, as images. Before they actually move, I need to find a way to check the four slots/cells next to that particular cell (North, South, East, West) to see if there is a food source in it, and if true, move to that cell. If false try the next direction, or if all false, just move randomly.

目前他们只是随机移动,如果幸运的话,有一个食物来源细胞,然后他们会吃。这就是我目前所拥有的,它确实有效

At the moment they just move randomly, and if by luck there is a food source on the cell, they will then eat. This is what I currently have, which does work

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

这是如果细胞含有食物来源,则称为进食部分的单独方法。我只是不确定如何在移动之前检查四个单元格?

There's a separate method for the 'eating' part, which will be called, if the cell contains a food source. I'm just not sure how I can make it check the four cells before moving?

推荐答案

为了我的工作解决方案,我建议你要从 TilePane 切换到 GridPane

For my solution to work I suggest you to switch from TilePane to GridPane.

你可以轻松绘制元素的网格:

You can easily draw a grid of your elements:

GridPane grid = new GridPane();
grid.add(child, columnIndex, rowIndex);

稍后会检测特定单元格中是否存在某些内容,例如使用此帮助程序:

And later on detect if something is in a specific cell, for example with this helper:

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

这篇关于Javafx如何通过tilePane移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆