qq_38326782 2017-05-09 05:12 采纳率: 0%
浏览 535

javafx相同的调用函数被跳过执行

这是一个想要实现三消的源代码(主要是想把三消游戏的小方块用按钮来处理,然后点击后为相应按钮设置一条轨迹使之运动),但是遇到了如下问题。
if(((twoButtonsPos.get(0)==twoButtonsPos.get(2))&&(Math.abs((twoButtonsPos.get(1)-twoButtonsPos.get(3)))==1))||((twoButtonsPos.get(1)==twoButtonsPos.get(3))&&(Math.abs((twoButtonsPos.get(0)-twoButtonsPos.get(2)))==1))){
ExchangableButton.ExchangeButton(gameView, primaryStage, scene, pane);
gameView[twoButtonsPos.get(0)][twoButtonsPos.get(1)]=twoButtons.get(1);
gameView[twoButtonsPos.get(2)][twoButtonsPos.get(3)]=twoButtons.get(0);
//交换的动画和实际引用的交换同时发生
if(DetectMatch.ExistMatch(gameView)){
gameView[twoButtonsPos.get(0)][twoButtonsPos.get(1)].removable=true;
gameView[twoButtonsPos.get(2)][twoButtonsPos.get(3)].removable=true;//这两行应该来说也是不需要的,在DetectMatch.ExustMatch里就已经解决了
return true;}
else{
ExchangableButton.ExchangeButton(gameView, primaryStage, scene, pane);
gameView[twoButtonsPos.get(0)][twoButtonsPos.get(1)]=twoButtons.get(0);
gameView[twoButtonsPos.get(2)][twoButtonsPos.get(3)]=twoButtons.get(1);//交换的动画和实际引用的交换同时发生
gameView[twoButtonsPos.get(0)][twoButtonsPos.get(1)].clicked=false;
gameView[twoButtonsPos.get(2)][twoButtonsPos.get(3)].clicked=false;
return false;
}
}
else{
gameView[twoButtonsPos.get(0)][twoButtonsPos.get(1)].clicked=false;
gameView[twoButtonsPos.get(2)][twoButtonsPos.get(3)].clicked=false;
return false;}
其中第一个if else是判断点击的两个按钮是否相邻,如果相邻就把两个按钮的位置交换
Line path1=new Line(40,40,(40+(temp.get(1).col-temp.get(0).col)*80),(40+(temp.get(1).row-temp.get(0).row)*80));
Line path2=new Line(40,40,(40-(temp.get(1).col-temp.get(0).col)*80),(40-(temp.get(1).row-temp.get(0).row)*80));
//每个节点的轨迹的开始和结束坐标都是相对于自己的中心点的坐标而言的

    path1.setStroke(Color.BLACK);
    path2.setStroke(Color.BLACK);

    PathTransition pt1=new PathTransition();
    pt1.setPath(path1);
    pt1.setNode(gameView[temp.get(0).row][temp.get(0).col]);
    pt1.setOrientation(OrientationType.NONE);
    pt1.setDuration(Duration.seconds(3));
    pt1.play();


    PathTransition pt2=new PathTransition();
    pt2.setPath(path2);
    pt2.setNode(gameView[temp.get(1).row][temp.get(1).col]);
    pt2.setOrientation(OrientationType.NONE);
    pt2.setDuration(Duration.seconds(3));
    pt2.play();

    primaryStage.show();

这是交换动画的源码。gameView是我用来储存按钮的数组。交换之后再用我自己写的判断是否存在满足条件的三个button 可以消除,如果可以就消除,不能的话就再调用函数换回去。
但是运行的时候出现了问题,就是我点了两个相邻但是交换后不能消除的button后,它会直接跳过 不执行第一个if判断里的动画,而是直接执行第二个if else 判断的else的动画

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-05 20:34
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这段代码的问题在于没有正确地使用if语句和else语句。当你尝试将一个按钮替换到另一个按钮时,你应该在交换之前先检查这两个按钮是否满足匹配条件。这样,即使你没有找到匹配的按钮,你也能够在交换过程中避免不必要的动画。

    首先,你需要更新你的逻辑以确保在进行交换操作之前,你需要先检查两个按钮是否相匹配:

    if (((twoButtonsPos.get(0) == twoButtonsPos.get(2)) && (Math.abs(twoButtonsPos.get(1) - twoButtonsPos.get(3)) == 1))
       || ((twoButtonsPos.get(1) == twoButtonsPos.get(3)) && (Math.abs(twoButtonsPos.get(0) - twoButtonsPos.get(2)) == 1))) {
       // ...
    }
    
    if (!DetectMatch.ExistMatch(gameView)) {
       // ...
    }
    

    在这个情况下,你需要先检查两个按钮是否相匹配。如果你找到了匹配的按钮,你可以继续执行交换操作;否则,你就不会执行任何交换操作,并且可以直接返回false,表示无法消除当前的匹配。

    接下来,你需要更新你的ExchangeButton类中的逻辑,以便在进行交换操作前先检查匹配条件:

    public void ExchangeButton(GameView gameView, Stage primaryStage, Scene scene, Pane pane) {
       Button buttonA = gameView[buttonARow][buttonACol];
       Button buttonB = gameView[buttonBRow][buttonBCol];
    
       if (((twoButtonsPos.get(0) == twoButtonsPos.get(2)) && (Math.abs(twoButtonsPos.get(1) - twoButtonsPos.get(3)) == 1))
          || ((twoButtonsPos.get(1) == twoButtonsPos.get(3)) && (Math.abs(twoButtonsPos.get(0) - twoButtonsPos.get(2)) == 1))) {
    
           buttonA.clicked = false;
           buttonB.clicked = false;
    
           // ...
    
           // 如果匹配成功,那么你可以在这里添加更多的动画或效果
           // ...
       }
    }
    

    现在,当两个按钮不匹配时,你会立即返回false,而不是执行交换操作并跳过后续的动画。

    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格