将click事件添加到javafx对象以在类外部运行函数

时间:2018-01-23 16:21:02

标签: java javafx state state-machine

所以我试图让状态机工作 - 不同的状态会导致不同的场景渲染 - 点击这些场景中的对象会改变场景。

这是状态机的一部分:

AnimationTimer animator = new AnimationTimer() {

            public void handle(long now) {

                switch (currentState) {
                case TOWN:
                    //Update
                    //Render
                    Town.showTownUI(stage);
                    break;
                case FOREST:
                    //Update
                    //Render
                    Forest.showForestUI(stage);
                    break;
                default:
                    //Update
                    //Render
                    break;
                }

            }
}

我在主类中有一个改变状态的函数:

public void changeState(State State) {
    this.currentState = State;
}

作为一个例子,这里是森林类。

public class Forest {
    Group forestGroup = new Group();
    Scene forest;

    public void makeUI(Stage stage) {

        // Test Circle
        Circle circle = new Circle();
        circle.setCenterX(100);
        circle.setCenterY(200);
        circle.setRadius(40);
        circle.setFill(Color.RED);

        forestGroup.getChildren().addAll(circle);
        forest = new Scene(forestGroup, 600, 500);

    }

    public void showForestUI(Stage stage) {
        stage.setScene(forest);
    }

}

我遇到的麻烦是试图将一个事件应用到“圈子”上。调用changeState函数 - 因为它不在定义makeUI的同一个类中。有没有办法以这种方式绑定或委托事件?

0 个答案:

没有答案