gpt4 book ai didi

java - 如何在javafx中拥有另一个场景

转载 作者:行者123 更新时间:2023-12-03 23:14:50 26 4
gpt4 key购买 nike

我正在写一个小游戏。我希望在游戏中间按下 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);
});

但是它不起作用,不透明度没有变回正常,奇怪的是,当我检查盒子上的声音时,播放了音乐,但盒子没有像一切正常一样被检查,但 View 被卡住。

最佳答案

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

考虑改用 Popup在现有窗口的顶部显示 pauseMenu,或者只使用带有未装饰 StageStyle 的模态 Stage,如下面的 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);
}
}

关于java - 如何在javafx中拥有另一个场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com