我正在写一个类似于Bejeweled的游戏,并且在运行应用程序一段时间后,块无法完全在视觉上正确的位置,但逻辑上它们是在正确的地方。情况如下图所示。JavaFX过渡动画无法将gridpane的子项放置在正确的位置
我不知道错误的原因。
这里是擦除和下降方法的代码。从逻辑上讲,这些方法表现良好
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();
});
}
}
我想我理解你的意思,但我想知道如何让Gridpane在转换后不改变它的子版本layoutX?谢谢! –
我成功解决了这个问题。谢谢! –
@AndyLee如果你能回答你自己的问题,并与他人分享如何解决问题,那将是一件好事。我所做的只是给你一个你可以放大和缩小的方向。 :) – Jai