我正在尝试为游戏添加子弹射击功能。子弹射击,但每次将子弹添加到场景中时,它都会在 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
提交
取消