场景不会更新javaFX

时间:2018-04-08 20:44:36

标签: java javafx

我在javafx中制作简单的游戏。我遇到的问题是当我试图用左上按钮向上控制火箭时,它没有更新火箭的位置(lander.rocket())。x,y坐标火箭总是零而且没有变化。请参阅code.thanks

//主要课程

Lander lander = new Lander();

Scene scene = new Scene(mainPane(), WIDTH, HEIGHT);


void Control_Rocket() {

   scene.setOnKeyPressed(e -> {

        switch (e.getCode()) {
            case RIGHT:
                lander.rocket().setTranslateX(lander.rocket().getTranslateX()+10);
                break;
            case LEFT:
                lander.rocket().setTranslateX(lander.rocket().getTranslateX()-10);
                break;
            case UP:
                lander.rocket().setTranslateY(lander.rocket().getTranslateY()-10);
                break;
            case DOWN:
                lander.rocket().setTranslateY(lander.rocket().getTranslateY()+10);
                break;
        }
        System.out.println("X:"+lander.rocket().getTranslateX()+" Y"+lander.rocket().getTranslateY());
    });

}

public Pane mainPane(){
    Pane pane = new Pane();
    pane.getChildren().add(Moon.background());
    pane.getChildren().addAll(Moon.hills());
    pane.getChildren().add(lander.rocket());
    return pane;
}

@Override
public void start(Stage primaryStage) {

    Control_Rocket();

    primaryStage.setTitle("Lunar-Lander!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

// Lander Class

public class Lander {

    double SPEED,UP_THRUST,GRAVITY,STEER_LEFT,STEER_RIGHT;

    private static final int _WIDTH = Main.WIDTH,
                             _HEIGHT = Main.HEIGHT;

    Rectangle rocket() {

        Rectangle rocket = new Rectangle(100,100, 10, 30);
        rocket.setFill(Color.RED);

        return rocket;
    }

}

0 个答案:

没有答案