亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每次添加到布局時,JavaFX 圖像都會落在 Y 軸上

每次添加到布局時,JavaFX 圖像都會落在 Y 軸上

森欄 2023-06-08 20:44:37
我正在嘗試為游戲添加子彈射擊功能。子彈射擊,但每次將子彈添加到場景中時,它都會在 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!它仍然不起作用,我不知道為什么。
查看完整描述

3 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

對于這種絕對定位,使用任何類型的布局窗格(如 HBox)都不是一個好主意,因為這總是會干擾您的定位。最好在這里只使用一個不做任何布局的窗格。



查看完整回答
反對 回復 2023-06-08
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

你認為這里發生了什么?

bullet.setTranslateX(imgView.getTranslateX() + 110);
bullet.setTranslateY(imgView.getTranslateY() - 57.5);

由于這些值永遠不會重置(例如設置為 0.0),因此它們每次都會相加。


查看完整回答
反對 回復 2023-06-08
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

答案是使用 Pane 而不是 VBox。在嘗試彌補使用 VBox 之后,通過一些額外的位置編輯來重新調整,我終于讓槍在相同的 Y 位置發射每顆子彈。感謝所有提供幫助的人!:)



查看完整回答
反對 回復 2023-06-08
  • 3 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號