首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX转换动画未能将网格窗格的子元素放置在正确的位置

JavaFX转换动画未能将网格窗格的子元素放置在正确的位置
EN

Stack Overflow用户
提问于 2017-05-07 15:51:35
回答 2查看 431关注 0票数 2

我正在写一个类似于Be宝石的游戏,在运行应用程序一段时间之后,这些块在视觉上并没有准确地定位到正确的位置,但从逻辑上讲,它们是在正确的位置。情况如下所示。

在这里输入图像描述

我不知道臭虫的原因。

下面是擦除和下降方法的代码。从逻辑上讲,这些方法做得很好。

代码语言:javascript
复制
public void erase()
{
    for (int i = 0; i < BlockManager.length; i++)
    {
        Block block = BlockManager.blocks[BlockManager.erased[i][0]][BlockManager.erased[i][1]];
        BlockManager.blocks[BlockManager.erased[i][0]][BlockManager.erased[i][1]] = null;

        FadeTransition transition = new FadeTransition(Duration.seconds(1), block);
        transition.setFromValue(1);
        transition.setToValue(0);

        transition.setOnFinished(e ->
        {

            blockGridPan.getChildren().remove(block);

            descend();

            BlockManager.resetArrays();

        });

        transition.play();

    }

}

int[][] descend = new int[HEIGHT * WIDE][2];
int descendLength = 0;


public void descend()
{
    int[] columnX = new int[10];
    int[] theUpperOne = new int[10];

    Arrays.fill(theUpperOne, 10);
    Arrays.fill(columnX, 0);

    for (int j = 0; j < BlockManager.length; j++)
    {
        columnX[BlockManager.erased[j][0]]++;
        if (BlockManager.erased[j][1] < theUpperOne[BlockManager.erased[j][0]])
        {
            theUpperOne[BlockManager.erased[j][0]] = BlockManager.erased[j][1];
        }
    }

    TranslateTransition transition = null;

    for (int i = 0; i < 10; i++)
    { 
        if (columnX[i] == 0)
            continue;

        for (int j = WIDE - 1; j >= 0; j--)
        {
            if (BlockManager.blocks[i][j] == null)
                continue;
            int dY = 0;
            for (int k = j + 1; k < WIDE; k++)
            {
                if (BlockManager.blocks[i][k] == null)
                    dY++;
            }
            if (dY != 0)
            {
                int deltaY = dY * 60;

                transition = new TranslateTransition(Duration.seconds(1), BlockManager.blocks[i][j]);
                transition.setByY(deltaY);

                BlockManager.blocks[i][j + dY] = BlockManager.blocks[i][j];
                BlockManager.blocks[i][j + dY].setPosition(i, j + dY);
                BlockManager.blocks[i][j] = null;

                BlockManager.blocks[i][j + dY].setDescended(true);
                transition.play();
            }
        }


        for (int j = columnX[i] - 1; j >= 0; j--)
        {

            int deltYJ = j * 60;
            createOneBlock(i, j);
            BlockManager.setBlockColorWithoutCheck(BlockManager.blocks[i][j]);
            blockGridPane.add(BlockManager.blocks[i][j], i, 0);
            System.out.println("show it");

            BlockManager.blocks[i][j].setDescended(true);

            transition = new TranslateTransition(Duration.seconds(1), BlockManager.blocks[i][j]);
            transition.setByY(deltYJ);
            transition.play();

        }

    }

    if (transition != null)
        transition.setOnFinished(e ->
        {

            BlockManager.resetArrays();
            if (BlockManager.check())
                erase();

        });

}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-08 06:05:19

你没有粘贴你的版面代码,所以我来猜一猜。

当您执行translate时,有一个潜在的问题,您的布局依赖于boundsInParent。这是来自JavaDoc的摘要:

这个节点的矩形界,包括它的变换。boundsInParent是通过取本地界(由boundsInLocal定义)并应用通过设置以下附加变量创建的转换来计算的。

  1. 变换ObservableList
  2. scaleX,scaleY
  3. 旋转
  4. layoutX,layoutY
  5. translateX,translateY

当您的TranslateTransition动画时,它正在更改块的translateY值,如果此时调用布局,则此转换值将影响块的位置。如果您的布局是由某种类型的布局管理器(即窗格)管理的,则需要检查它们如何定位其子节点。

票数 0
EN

Stack Overflow用户

发布于 2017-05-15 08:08:07

我使用网格窗格来定位我的块,最初有10行10列,并且在块下降之后,网格窗格自动添加一行(因为该块没有准确地转换到它应该转换的位置,而且我仍然不知道原因)。我更改html文档以避免列的增长。而且它是有效的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43833503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档