从不同的类更改javafx场景

时间:2018-04-09 20:33:34

标签: java javafx nullpointerexception

我有一个迷宫游戏,玩家正在迷宫中移动,试图将其推向终点,当玩家进入窗口时,游戏将被取代为胜利者屏幕,该屏幕上显示“胜利者”且有“退出“按钮,但我正在努力制作舞台变化的场景。

这是结尾图块的相关代码:

public class EndTile extends MazeRoute{

    public void MoveHere(Player Player){
    Player.setXpos(getTileXpos());
    Player.setYpos(getTileYpos());

    MazeGame.primaryStage.setScene(MazeGame.winnerScene);

这是我游戏初级阶段的相关部分

public class MazeGame extends Application {
    public static GridPane winnerLayout = new GridPane();
    public static Stage primaryStage;
    public static Scene winnerScene = new Scene(winnerLayout);


    public void start(Stage primaryStage){
        try{
            GridPane root = new GridPane();
            Scene scene = new Scene(root, Color.BLACK);

            Text winnerText = new Text("you win!");
            winnerLayout.getChildren().add(winnerText);

            primaryStage.setTitle("MazeGame");
            primaryStage.setScene(scene);
            primaryStage.show();

基本上我问我如何使用primaryStage中的moveHere方法更改EndTile中的场景。

我现在正在使用的内容在制作到最终拼贴时为我提供NullPointerException

编辑:将MazeGame.primaryStage = primaryStage;添加到我的启动方法中,@ fabian在评论中建议,这解决了问题。

0 个答案:

没有答案