JavaFX中的java事件处理程序突然停止工作
我有一个简单的JavaFX程序,可以移动形状。移动由按键控制,移动选项有左、右和跳跃。程序正常运行,只是有时事件处理程序会死机。到目前为止,我发现的唯一一致性是,它只发生在左移之后,但我看不到它发生的任何模式
这种行为怎么可能发生?我的密码坏了吗
更新:我用shape.setFocusTraversable(true);
替换了shape.requestFocus();
,这有点帮助:现在我可以在形状冻结后向右移动,但不能向左移动。另外,如果我在“解冻”后将其向左移动,我可以执行跳转。向左移动将再次冻结该形状
注:形状为Rectangle shape = new Rectangle();
处理程序:
shape.setFocusTraversable(true);
shape.setOnKeyPress((e) -> {
if(e.getCode() == KeyCode.RIGHT) {
shape.moveRight();
}
else if(e.getCode() == KeyCode.LEFT) {
shape.moveLeft();
}
else if(e.getCode() == KeyCode.SPACE) {
shape.jump();
}
});
移动方法:
public void moveLeft() {
shape.setX(shape.getX() - 3);
}
public void moveRight() {
shape.setX(shape.getX() + 3);
}
public void jump() {
Arc path = new Arc();
path.setCenterX(shape.getX() + shape.getWidth() / 2 + 20);
path.setCenterY(shape.getY() + shape.getHeight() / 2);
path.setRadiusX(40);
path.setRadiusY(80);
path.setStartAngle(180);
path.setLength(-180);
PathTransition jump = new PathTransition();
jump.setPath(path);
jump.setNode(shape);
jump.setDuration(Duration.millis(1500));
jump.play();
shape.setX(path.getCenterX() + path.getRadiusX() + 15);
}
共 (0) 个答案