当前位置: 首页 > 知识库问答 >
问题:

JavaFXGUI通过切换场景而冻结

冯浩旷
2023-03-14

我正在将游戏的场景从启动页面切换到游戏,如果我单独玩,游戏场景效果很好,但当我从启动页面切换后玩游戏时,游戏会冻结一段时间。

是什么原因导致我的主游戏在切换场景后冻结了一段时间(一段时间后再次启动),我确信游戏在没有切换场景的情况下运行良好。

程序的控制流程是——从播放器开始,startuppage场景被加载到舞台,如果按下播放按钮,则在主控制器中,场景被切换到我的游戏场景。问题只会在切换场景时出现。请帮忙。提前谢谢。

这是我的主力队员

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class player extends Application {
    Scene s1,s2;
    Stage stg;
public static void main(String args[]) {
        Application.launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    stg = stage;
    FXMLLoader loader1 = new FXMLLoader(getClass().getResource("Main.fxml"));
    Parent root1 = loader1.load();

    s1 = new Scene(root1,800,600);

    s1.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
    stg.setScene(s1);

    stg.show(); 

}
}

这是我的启动页面控制器。

package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class mainController   {
    @FXML 
    Button play;


public void func(ActionEvent event) throws Exception {
    FXMLLoader loader = new FXMLLoader (getClass().getResource("thread1.fxml"));
    Parent root = loader.load();
    Scene s2 = new Scene(root,800,600);
    Stage window = (Stage)(((Node)event.getSource()).getScene().getWindow());
    window.setScene(s2);
    window.show();
}
}

这是我的启动页面fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<Pane id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.mainController">
   <children>
      <ImageView id="menubg" fitHeight="1193.0" fitWidth="763.0" layoutY="-231.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@background.png" />
         </image>
      </ImageView>
      <Button fx:id="play" layoutX="265.0" layoutY="322.0" mnemonicParsing="false" onAction="#func" prefHeight="40.0" prefWidth="111.0" text="Play Game" textAlignment="CENTER" />
      <Button fx:id="help" layoutX="241.0" layoutY="483.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="58.0" text="Help" textAlignment="CENTER" />
      <Button fx:id="exit" layoutX="349.0" layoutY="483.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="58.0" text="Exit" textAlignment="CENTER" />
      <Text id="title" layoutX="177.0" layoutY="231.0" strokeType="OUTSIDE" strokeWidth="0.0" text="PARACHUTE PANIC" textAlignment="CENTER">
         <font>
            <Font name="AR DARLING" size="31.0" />
         </font>
      </Text>
      <Text id="highscore" layoutX="254.0" layoutY="433.0" strokeType="OUTSIDE" strokeWidth="0.0" text="HIGHSCORE: 40">
         <font>
            <Font name="AR DARLING" size="17.0" />
         </font>
      </Text>
   </children>
</Pane>

游戏控制器类太大无法上传

这是我的游戏的fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <children>
      <ImageView fx:id="background" fitHeight="1094.0" fitWidth="731.0" layoutX="-2.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@background.png" />
         </image>
      </ImageView>
      <ImageView fx:id="player1" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefall.png" />
         </image>
      </ImageView>
        <ImageView fx:id="playero1" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefallp.png" />
         </image>
      </ImageView>
        <ImageView fx:id="player2" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefall.png" />
         </image>
      </ImageView>
        <ImageView fx:id="playero2" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefallp.png" />
         </image>
      </ImageView>
       <ImageView fx:id="player3" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefall.png" />
         </image>
      </ImageView>
        <ImageView fx:id="playero3" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefallp.png" />
         </image>
      </ImageView> 
      <ImageView fx:id="player4" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefall.png" />
         </image>
      </ImageView>
        <ImageView fx:id="playero4" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefallp.png" />
         </image>
      </ImageView> 
      <ImageView fx:id="player5" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefall.png" />
         </image>
      </ImageView>
        <ImageView fx:id="playero5" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefallp.png" />
         </image>
      </ImageView> 
      <ImageView fx:id="player6" fitHeight="42.0" fitWidth="98.0" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefall.png" />
         </image>
      </ImageView>
        <ImageView fx:id="playero6" fitHeight="80" fitWidth="80" layoutX="47.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@freefallp.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud1" fitHeight="59.0" fitWidth="118.0" layoutX="-7.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud3" fitHeight="59.0" fitWidth="118.0" layoutX="150.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud4" fitHeight="59.0" fitWidth="118.0" layoutX="250.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud6" fitHeight="59.0" fitWidth="118.0" layoutX="340.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud7" fitHeight="59.0" fitWidth="118.0" layoutX="440.0" layoutY="-6.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud8" fitHeight="59.0" fitWidth="118.0" layoutX="540.0" layoutY="-6.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud9" fitHeight="59.0" fitWidth="118.0" layoutX="640.0" layoutY="-6.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
       <ImageView fx:id="boat" fitHeight="160" fitWidth="160" layoutX="0.0" layoutY="665.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@boat.png" />
         </image>
      </ImageView>
      <ImageView fx:id="water1" fitHeight="95.0" fitWidth="400.0" layoutX="386.0" layoutY="680.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@water.gif" />
         </image>
      </ImageView>
      <ImageView fx:id="water" fitHeight="95.0" fitWidth="400.0" layoutX="-10.0" layoutY="683.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@water.gif" />
         </image>
      </ImageView> 
      <ImageView fx:id="plane" fitHeight="95" fitWidth="95" layoutX="650.0" layoutY="31.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@airplane.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud2" fitHeight="59.0" fitWidth="118.0" layoutX="60.0" layoutY="-5.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud5" fitHeight="59.0" fitWidth="118.0" layoutX="190.0" layoutY="-17.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <ImageView fx:id="cloud10" fitHeight="59.0" fitWidth="118.0" layoutX="590.0" layoutY="-9.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@cloud.png" />
         </image>
      </ImageView>
      <Label fx:id="lives" layoutX="14.0" layoutY="6.0" text="Label">
         <font>
            <Font name="AR DARLING" size="18.0" />
         </font></Label>
      <Label fx:id="score" layoutX="579.0" layoutY="6.0" text="Label">
         <font>
            <Font name="AR DARLING" size="18.0" />
         </font></Label>
   </children>
</AnchorPane>

共有1个答案

郏志学
2023-03-14

我猜加载thread1。单击按钮后,从JavaFX线程内部的fxml可能是问题所在。

你可以尝试在点击按钮之前加载它,这样不会花费太长时间。

游戏者爪哇:

public class player extends Application {
    Scene s1,s2;
    Stage stg;
    public static void main(String args[]) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stg = stage;
        FXMLLoader loader1 = new FXMLLoader(getClass().getResource("Main.fxml"));
        Parent root1 = loader1.load();
        s1 = new Scene(root1,800,600);
        s1.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
        stg.setScene(s1);

        FXMLLoader loader2 = new FXMLLoader (getClass().getResource("thread1.fxml"));
        Parent thread1Root = loader2.load();
        s2 = new Scene(thread1Root,800,600);
        ((MainController)loader2.getController()).setThread1Scene(s2);

        stg.show(); 
    }
}

主控制器。爪哇:

public class MainController {
    @FXML 
    Button play;
    private Scene thread1Scene;

    public void setThread1Scene(Scene s2) {
        this.thread1Scene = s2;
    }

    public void func(ActionEvent event) throws Exception {
        Stage window = (Stage)(((Node)event.getSource()).getScene().getWindow());
        window.setScene(s2);
        window.show();
    }
}

您也可以在点击按钮时显示“加载”屏幕,然后加载thread1。fxml在平台内。runLater()

showLoadingLabel();
Platform.runLater(() -> doSomethingSlow());

 类似资料:
  • 当我运行该程序时,它加载第一个和第二个fxml,但继续显示第一个fxml(当它应该显示第二个时)。我可以使用相同代码的输入(即按钮)从第一个屏幕到第二个屏幕。 我尝试使用不同的加载器,每次设置场景时都使用。show()方法,但这些(或它们的许多变体)都不起作用。 我在调用方法时进行打印--这就是我确定它们被加载的方式(printline): null 相反,输出(在调试中,如果stage.show

  • 开始一个新游戏,改变关卡,或结束游戏时,为了给用户不同的效果呈现,大多需要切换不同的场景。Cocos2d-x 提供了一系列方式去做这件事情 场景切换。 场景切换的方式 有很多场景切换的方式,每种都有特定的方法,让我们来看看: auto myScene = Scene::create(); runWithScene() 用于开始游戏,加载第一个场景。只用于第一个场景! Director::getI

  • 我在这里阅读了几个与我的问题相关的问题/解决方案。但似乎什么都不管用。 所以我有一个全屏模式的primarystage,比如说,如果我点击一个按钮,它会改变场景。但舞台似乎显示了任务栏。我还通过将此添加到所有场景方法中解决了此问题。。 但是,场景中的过渡不是那么流畅。首先,它进入桌面,然后回到全屏...这不是理想的解决方案。 以下是我的初级阶段代码: 这是我改变场景的代码: 我不知道这是虫子还是什

  • 我创建了一个游戏,我想给它添加一个开始屏幕,我使用FXML添加了它,还添加了两个按钮(开始和退出)。 按下开始按钮后,我希望游戏加载场景并切换到游戏开始。我对如何做有一个粗略的想法,但我有点挣扎,因为我的SampleController类不知道如何启动游戏等,因为所有代码(以及加载初始开始菜单的代码)都在我的主类中,所以我尝试了这样的事情: 我尝试使用一个函数来切换场景,但它不起作用,也试图使用获

  • 我在class1中创建了一个场景,然后在Class2中创建了一个scene2。如何在两者之间切换? 这是第二节课,我有另一个场景。