2017-05-07 98 views
2

我正在写一个类似于Bejeweled的游戏,并且在运行应用程序一段时间后,块无法完全在视觉上正确的位置,但逻辑上它们是在正确的地方。情况如下图所示。JavaFX过渡动画无法将gridpane的子项放置在正确的位置

enter image description here

我不知道错误的原因。

这里是擦除和下降方法的代码。从逻辑上讲,这些方法表现良好

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

     }); 

} 

}

回答

0

你没有粘贴代码为你的布局,所以我打算做一个猜测。

当你做translate和你的布局取决于boundsInParent有一个潜在的问题。这是来自JavaDoc的摘要:

该节点的矩形边界包括其变换。 boundsInParent通过取本地边界(由 boundsInLocal定义)和应用该变换通过设置 以下附加变量

  1. 创建计算变换ObservableList
  2. 的scaleX,的scaleY
  3. 旋转
  4. layoutX, layoutY
  5. translateX,translateY

当你的TranslateTransition动画,它正在改变块的translateY值,如果此时你的布局被调用,这个转换值将会影响块的定位方式。如果您的布局是由某种布局管理器(即窗格)管理的,则需要检查它们如何放置子项。

+0

我想我理解你的意思,但我想知道如何让Gridpane在转换后不改变它的子版本layoutX?谢谢! –

+0

我成功解决了这个问题。谢谢! –

+0

@AndyLee如果你能回答你自己的问题,并与他人分享如何解决问题,那将是一件好事。我所做的只是给你一个你可以放大和缩小的方向。 :) – Jai

0

我用gridpane来定位我的块,有10行10列开始和gridpane自动添加一行块已经开始下降,(因为该块不完全翻译的位置,它应该和我仍然不知道它的原因)。我更改了html文档以避免列的增长。它的工作原理。

相关问题