gpt4 book ai didi

java - JavaFX 中的键盘箭头

转载 作者:行者123 更新时间:2023-11-30 02:53:55 25 4
gpt4 key购买 nike

又是我!我有一门大炮,现在我正在尝试用键盘的箭头移动它。我不明白为什么代码不起作用?我哪里错了?谢谢!

public class SchermataGiocoController {

private Parent Menu, Avvio;
private TranslateTransition tt;
private Cannone cannone;
private Aereo aereo;
private Proiettile proiettile;
private RotateTransition rt;

@FXML
private Circle circle;

@FXML
private Button exit;

@FXML
private ImageView cannone_im;

@FXML
private ImageView carro;

@FXML
private AnchorPane SchermataGioco;

@FXML
private ImageView aereo_im;

@FXML
private Button menu;

@FXML
private Button home;

@FXML
void initialize() {
assert exit != null : "fx:id=\"exit\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert cannone_im != null : "fx:id=\"cannone_im\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert carro != null : "fx:id=\"carro\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert SchermataGioco != null : "fx:id=\"SchermataGioco\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert aereo_im != null : "fx:id=\"aereo_im\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert up != null : "fx:id=\"up\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert menu != null : "fx:id=\"menu\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert down != null : "fx:id=\"down\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
assert home != null : "fx:id=\"home\" was not injected: check your FXML file 'SchermataGioco.fxml'.";

//creazione di un oggetto Cannone
cannone = new Cannone(129, 96, 0, 340); //Cannone = cannone
//creazione di un oggetto Proiettile
proiettile = new Proiettile(16, 16, 0, 340); //proiettile = bullet
//inizializzazione dei vari metodi
TranslateTransition();
moveUp();
moveDown();
goHome();
}
//Method to change scene and back to home
public void goHome() {
home.setOnAction((ActionEvent event) -> {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Game.class.getResource("/game/view/Avvio.fxml"));

Avvio = (Parent) loader.load();
home.getScene().getWindow().hide();
} catch (IOException ioe) {
ioe.getMessage();
}
Stage stage = new Stage();
stage.setScene(new Scene(Avvio));
stage.show();
});
}
@FXML
public void moveUp() {
cannone_im.getScene().setOnKeyPressed((KeyEvent ke) -> {
if (ke.getCode().equals(KeyCode.RIGHT)) {
rt = new RotateTransition(Duration.millis(100), cannone_im);
if (cannone_im.getRotate() > -70) {
rt.setAxis(Z_AXIS);
rt.setByAngle(cannone_im.getRotate());
rt.setToAngle(cannone_im.getRotate() - 5);
} else {
rt.setToAngle(-70);
}
}
rt.play();
});
}

错误是 root 不能为空。谢谢!

最佳答案

更新后:您的代码有几个问题。

1) Node获得焦点时会触发按键事件

您的ImageView不会获得焦点,因此KeyEvent永远不会被触发。

解决方案:将监听器直接添加到您的场景

这个答案是一个很好的引用:How to write a KeyListener for JavaFX

2) 您不应使用 setOnKeyReleased

特别是在游戏中,如果当您释放按键时事件才触发,这真的很令人沮丧。

解决方案:改用setOnKeyPressed

3) 当您在初始化中调用 moveUp() 时,它不应具有 @FXML 注释

@FXML 注释表示您要将此函数用作 FXML 文件中的引用。但此方法只是创建监听器,而不是监听器本身。

解决方案:moveUp()中删除@FXML注释

与此问题相关:Lambda functions with FXML in JavaFX8

4) 在附加监听器之前,您应该确保 sceneProperty 不为 null

解决方案:监听ImageViewsceneProperty的变化。

最终的解决方案可能是这样的:

// The @FXML annotation is removed!
public void moveUp() {
cannone_im.sceneProperty().addListener(new ChangeListener<Scene>() {

@Override
public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene newValue) {

if(newValue != null){
cannone_im.getScene().setOnKeyPressed((KeyEvent ke) -> {
if (ke.getCode().equals(KeyCode.RIGHT)) {
rt = new RotateTransition(Duration.millis(100), cannone_im);
if (cannone_im.getRotate() > -70) {
rt.setAxis(Z_AXIS);
rt.setByAngle(cannone_im.getRotate());
rt.setToAngle(cannone_im.getRotate() - 5);
} else {
rt.setToAngle(-70);
}
}
rt.play();
});
}
}
});
}

关于java - JavaFX 中的键盘箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com