如何在javafx中有另一场景

问题描述:

我正在写一个小游戏。当我在游戏中间按下esc时,我想要在模糊的游戏菜单上显示一个暂停菜单。 我所做的是我做了一个新的场景,其中有一个stackPane包裹了过去的根和暂停菜单根,然后我设置了过去的根0.4的不透明度。如何在javafx中有另一场景

然后,当点击暂停菜单中的恢复按钮时,我将不透明度改回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); 
     }); 

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

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

考虑,而不是使用Popup显示在现有窗口的顶部pauseMenu,或只使用一个模式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); 
    } 
} 
+0

哇这是伟大的感谢:)但它不适合我,因为当游戏暂停时,暂停游戏按钮仍然有效。并可点击。 –

+0

在这种情况下,请使用模态阶段。查看更新版本。 –

+0

哦!有什么办法摆脱这种噪音?当游戏被点击时?我也不希望那些最小化和最大化,并关闭顶部的按钮不活动(我对不起,如果我有点技术) –