如何在javafx中拥有一个场景

时间:2016-06-08 11:18:56

标签: java javafx scene

我正在写一个小游戏。我希望在游戏中间按下esc时显示模糊游戏菜单上的暂停菜单。 我做的是我创建一个新场景,其中stackPane包裹过去的根和暂停菜单根,然后我设置过去根0.4的不透明度。

然后,当单击暂停菜单中的恢复按钮时,我将不透明度更改回1并在舞台上设置过去的场景,然后将其冻结。有谁知道为什么?任何人都可以帮我实现这个目标吗?

这是我制作新场景的部分,然后我把它放在舞台上:

        StackPane wrapper = new StackPane();
        previousScene = main.getPrimaryStage().getScene();
        previousScene.getRoot().setOpacity(.4);
        vBox.setId("pausedWrapper");
        wrapper.getChildren().add(previousScene.getRoot());
        wrapper.getChildren().add(vBox);
        scene = new Scene(wrapper, 1200, 700);
        return scene;

以下是我将其更改回原来的部分:

        resumeGame.setOnAction(event -> {
            System.out.println("game resumed!");
            previousScene.getRoot().setOpacity(1);
            main.getPrimaryStage().setScene(previousScene);
        });

然后它不起作用,不透明度不会恢复正常,奇怪的是当我检查音乐盒上的声音时播放音乐但是盒子没有得到检查,就像一切正常但视图被冻结了。

1 个答案:

答案 0 :(得分:8)

节点不能是两个不同场景图的一部分。这在代码中发生在previousScene的根目录中,因为它是previousScene和您在第一个代码块中创建的新场景的一部分。最有可能发生的事情是,当您将其添加到第二个场景时,它会从第一个场景中删除(尽管从您发布的代码中很难判断出来)。

请考虑使用Popup在现有窗口的顶部显示pauseMenu,或者只使用带有未修饰Stage的模式StageStyle,如下面的SSCCE :

import javafx.animation.Animation;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Modality;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public class PauseExample extends Application {

    @Override
    public void start(Stage primaryStage) {

        Rectangle rect = new Rectangle(50, 50, 50, 50);
        rect.setFill(Color.CORAL);

        TranslateTransition animation = createAnimation(rect);

        Button pauseButton = new Button("Pause");

        Pane pane = new Pane(rect);
        pane.setMinSize(600, 150);

        BorderPane root = new BorderPane(pane, null, null, pauseButton, new Label("This is\nthe main\nscene"));

        pauseButton.setOnAction(e -> {
            animation.pause();
            root.setEffect(new GaussianBlur());

            VBox pauseRoot = new VBox(5);
            pauseRoot.getChildren().add(new Label("Paused"));
            pauseRoot.setStyle("-fx-background-color: rgba(255, 255, 255, 0.8);");
            pauseRoot.setAlignment(Pos.CENTER);
            pauseRoot.setPadding(new Insets(20));

            Button resume = new Button("Resume");
            pauseRoot.getChildren().add(resume);

            Stage popupStage = new Stage(StageStyle.TRANSPARENT);
            popupStage.initOwner(primaryStage);
            popupStage.initModality(Modality.APPLICATION_MODAL);
            popupStage.setScene(new Scene(pauseRoot, Color.TRANSPARENT));


            resume.setOnAction(event -> {
                root.setEffect(null);
                animation.play();
                popupStage.hide();
            });

            popupStage.show();
        });

        BorderPane.setAlignment(pauseButton, Pos.CENTER);
        BorderPane.setMargin(pauseButton, new Insets(5));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TranslateTransition createAnimation(Rectangle rect) {
        TranslateTransition animation = new TranslateTransition(Duration.seconds(1), rect);
        animation.setByX(400);
        animation.setCycleCount(Animation.INDEFINITE);
        animation.setAutoReverse(true);
        animation.play();
        return animation;
    }

    public static void main(String[] args) {
        launch(args);
    }
}