我正在嘗試為游戲添加子彈射擊功能。子彈射擊,但每次將子彈添加到場景中時,它都會在 Y 軸上減小。我的目標是每次添加項目符號時都將其添加到布局中 X 軸和 Y 軸上的相同位置。我試圖通過使用進行檢查,if (!(layout.getChildren().contains(bullet))) {但它仍然沒有用。public class AnimationTest {static Stage primaryStage = new Stage();static VBox layout = new VBox(10);static Image image = new Image("/run-gun.png");static Image bulletI = new Image("/bullet.png");static ImageView bullet;public static void main(String[] args){}public static void start() throws Exception {imgView = new ImageView(image);bullet = new ImageView(bulletI);Scene scene = new Scene(layout); layout.getChildren().add(imgView); primaryStage.setScene(scene); primaryStage.show();scene.setOnKeyPressed(e -> {getKey(e);});}public static void getKey(KeyEvent e){bullet = new ImageView(bulletI);if (e.getCode() == KeyCode.SPACE) { if (layout.getChildren().contains(bullet)) { } else { layout.getChildren().add(bullet); bullet.setTranslateX(imgView.getTranslateX() + 110); bullet.setTranslateY(imgView.getTranslateY() - 57.5); posY = (int) bullet.getTranslateY(); bullet.setTranslateY(posY); // move bullet TranslateTransition tt = new TranslateTransition(Duration.millis(750), bullet); tt.setByX(1920); tt.setCycleCount(1); tt.setAutoReverse(false); tt.setOnFinished(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { try { removeBullet(); } catch (Exception e) { e.printStackTrace(); } } }); tt.play(); } } else { }}}I want the bullet image to ALWAYS be added to the layout in the same X and Y positions. Thanks!它仍然不起作用,我不知道為什么。
添加回答
舉報
0/150
提交
取消