Java 我的形状移动了,但会暂停一会儿

Java 我的形状移动了,但会暂停一会儿,java,animation,javafx,Java,Animation,Javafx,我已经用JavaFX编写了一个简单的“游戏”。它基本上是一个左右移动的矩形。问题是,当按住某个键时,矩形会移动(良好),但在继续移动之前会暂停一段时间。有什么解决办法吗 p.S我是这个网站的新手 预包装样品; 导入javafx.application.application; 导入javafx.beans.property.SimpleDoubleProperty; 导入javafx.beans.property.SimpleIntegerProperty; 导入javafx.geometry.

我已经用JavaFX编写了一个简单的“游戏”。它基本上是一个左右移动的矩形。问题是,当按住某个键时,矩形会移动(良好),但在继续移动之前会暂停一段时间。有什么解决办法吗

p.S我是这个网站的新手 预包装样品; 导入javafx.application.application; 导入javafx.beans.property.SimpleDoubleProperty; 导入javafx.beans.property.SimpleIntegerProperty; 导入javafx.geometry.Pos; 导入javafx.scene.Group; 导入javafx.scene.scene; 导入javafx.scene.control.Button; 导入javafx.scene.control.Label; 导入javafx.scene.input.KeyCode; 导入javafx.scene.layout.HBox; 导入javafx.scene.paint.Color; 导入javafx.scene.shape.Rectangle; 导入javafx.stage.stage; 公共类主扩展应用程序 { 受保护的静态SimpleIntegerProperty sint_speed=新SimpleIntegerProperty(1); 受保护的静态双dbl_播放器W=140,dbl_播放器H=20; 受保护的静态SimpleDoubleProperty sdbl_playerX=新SimpleDoubleProperty(130),sdbl_playerY=新SimpleDoubleProperty(360); @凌驾 公共空间启动(舞台主舞台) { 组grp_main=新组(); 标签lbl_spd=新标签(); lbl_spd.textProperty().bind(sint_speed.asString()); 按钮btn_increaseSpd=新按钮(“”); btn_增加速度设置动作(e-> { 烧结速度设置(烧结速度intValue()+1); });
Button btn_decreaseSpd=new Button(“它与OnKeyPressed事件的触发方式有关。要可视化这一点,您可以按住键盘上的任何键,您会注意到第一个字符是如何出来的,并且在流的其余部分出来时暂停了很长时间

要解决此问题,请使用布尔标志:

isMovingLeft = false;
isMovingRight = false;

if(e.getCode() == KeyCode.LEFT){
     //instead of doing this:
     //sdbl_playerX.set(sdbl_playerX.doubleValue() - sint_speed.intValue());
     isMovingLeft = true;
}

OnKeyPressed(){
   if left is pressed:
       set isMovingLeft to true;
   if right is pressed:
       set isMovingRight to true;
}

onKeyReleased(){
   if left is released:
       set isMovingLeft to false;
   if right is pressed:
       set isMovingRight to false; 
}

//maingameloop i.e: handle method in JavaFX
handle(){
    if(isMovingLeft){
        //apply move left logic
    }
    if(isMovingRight){
        //apply move right logic
    }
}

除了@McKevin发布的内容外,您还可以使用
TranslateTransition
执行实际移动。只需在
onKeyPressed()
处理程序中启动转换,并在
onkeypreleased()
处理程序中停止转换。以下代码片段显示了如何在一个方向上执行此操作(当您的玩家达到某个极限时,您仍然需要添加左右之间的区别,并添加对角球情况的处理):


另请参见

我们不是读心术者。请发布相关代码。这很可能是由通常的按键行为造成的-在任何编辑器中连续按任意键,您将看到字母被键入,然后短暂停顿,然后才连续键入。很可能,您需要为
onKeyRel定义事件处理程序放松()
,然后在按下
onKeyPressed
,如果尚未启动,则启动移动,然后在
onKeyReleased()中
停止移动顺便说一句,您发布的代码无法编译-请发布@AndreasFester作为SSCCE,请参阅MCVE是SSCCE的特定版本,因此是首选。@MarkusWMahlberg谢谢-需要更新我的SO注释样板:)
isMovingLeft = false;
isMovingRight = false;

if(e.getCode() == KeyCode.LEFT){
     //instead of doing this:
     //sdbl_playerX.set(sdbl_playerX.doubleValue() - sint_speed.intValue());
     isMovingLeft = true;
}

OnKeyPressed(){
   if left is pressed:
       set isMovingLeft to true;
   if right is pressed:
       set isMovingRight to true;
}

onKeyReleased(){
   if left is released:
       set isMovingLeft to false;
   if right is pressed:
       set isMovingRight to false; 
}

//maingameloop i.e: handle method in JavaFX
handle(){
    if(isMovingLeft){
        //apply move left logic
    }
    if(isMovingRight){
        //apply move right logic
    }
}
...
private Rectangle rct_player;
private TranslateTransition transTransition;
private boolean isMoving = false;
...

    // Create the translate transition for the rectangle
    transTransition = new TranslateTransition(new Duration(75000), rct_player);
    transTransition.setToY(0);
    transTransition.setToX(1500);
    transTransition.setInterpolator(Interpolator.LINEAR);
    transTransition.setCycleCount(Timeline.INDEFINITE);

    scn_main.setOnKeyPressed(e-> {
        if (!isMoving) {
            transTransition.play();
            isMoving = true;
        }
    });
    scn_main.setOnKeyReleased(e -> {
        transTransition.stop(); 
        isMoving = false;
    });
...