JavaFX"移动"节点

时间:2017-04-28 19:22:20

标签: java javafx

我是JavaFX的新手,我遇到了问题。如果我只是移动我的"播放器"我试图用UP,DOWN,LEFT,RIGHT移动节点。节点,它的工作完美,但我想做一个像推箱子的游戏,所以我的玩家节点可以移动" Crate"节点,问题是如果我按下Crate旁边的RIGHT,它交换位置,但是这里的工作不正确是移动方法:

private void lepes(int i, int j, int target_i, int target_j){
      int ai, aj;
      int bi, bj;

      ai = i;
      aj = j;

      bi = target_i;
      bj = target_j;

      MyNode nodeA = playfield[ai][aj];
      nodeA.toFront();

      MyNode nodeB = playfield[bi][bj];
      nodeB.toFront();

      playfield[ai][aj] = nodeB;
      playfield[bi][bj] = nodeA;

      Path pathA = new Path();
      pathA.getElements().add (new MoveTo ( nodeA.getTranslateX() + nodeA.getBoundsInParent().getWidth() / 2.0, nodeA.getTranslateY() + nodeA.getBoundsInParent().getHeight() / 2.0));
      pathA.getElements().add (new LineTo( nodeB.getTranslateX() + nodeB.getBoundsInParent().getWidth() / 2.0, nodeB.getTranslateY() + nodeB.getBoundsInParent().getHeight() / 2.0));


      PathTransition pathTransitionA = new PathTransition(); 
      pathTransitionA.setDuration(Duration.millis(1));
      pathTransitionA.setNode( nodeA);
      pathTransitionA.setPath(pathA);

      pathTransitionA.play();

      Path pathB = new Path();
      pathB.getElements().add (new MoveTo ( nodeB.getTranslateX() + nodeB.getBoundsInParent().getWidth() / 2.0, nodeB.getTranslateY() + nodeB.getBoundsInParent().getHeight() / 2.0));
      pathB.getElements().add (new LineTo( nodeA.getTranslateX() + nodeA.getBoundsInParent().getWidth() / 2.0, nodeA.getTranslateY() + nodeA.getBoundsInParent().getHeight() / 2.0));

      PathTransition pathTransitionB = new PathTransition(); 
      pathTransitionB.setDuration(Duration.millis(1));
      pathTransitionB.setNode( nodeB);
      pathTransitionB.setPath(pathB);

      pathTransitionB.play();


      pathTransitionA.setOnFinished( new EventHandler<ActionEvent>() {

          @Override
          public void handle(ActionEvent event) {

              if( (pathTransitionB.getStatus() == Status.RUNNING))
                  return;
          }
      });

       pathTransitionB.setOnFinished( new EventHandler<ActionEvent>() {

          @Override
          public void handle(ActionEvent event) {

              if( (pathTransitionA.getStatus() == Status.RUNNING))
                  return;
          }
      });


}

这里是eventHandler

        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
          @Override
          public void handle(KeyEvent event) {
            if (event.getCode()==KeyCode.RIGHT) { 

                if(jatekosX < 8 && playfield[jatekosX+1][jatekosY].getHisNumber() == 2){
                    lepes(jatekosX, jatekosY, jatekosX+1, jatekosY);
                    jatekosX++;
                    lepesek++;
                }
                else if(jatekosX < 8 && playfield[jatekosX+1][jatekosY].getHisNumber() == 1 ){

                    lepes(jatekosX+1, jatekosY, jatekosX+2, jatekosY);          
                    lepes(jatekosX, jatekosY, jatekosX+1, jatekosY);
                    jatekosX++;
                    lepesek++; 

                }

                System.out.println("Jobbra");
                System.out.println("X: " + jatekosX + "Y: " + jatekosY + " lépés: " + lepesek + "\n");
            } 
          }       
        });

以下是crate节点https://postimg.org/image/7qkqxnxs5/

旁边的图片

这是在正确的步骤后https://postimg.org/image/t1ib1xfwl/

我不知道是什么原因引起的。

0 个答案:

没有答案