【发布时间】:2019-11-23 03:44:58
【问题描述】:
所以我目前正在 javaFX 中制作一个简单的回合制游戏,但遇到了问题。我试图在代码中放入一个while循环(在倒数第二个代码块中找到),但是当我执行程序时什么也没有运行。我没有收到任何错误,也没有弹出窗口。
如果我摆脱了我的 while 循环,那么 GUI 运行良好。
我试图移动 while 循环,但这并没有改变任何东西。我还尝试转到以前的构建以查看 while 循环是否可以在那里工作,但同样的问题也发生在它身上。
package application;
import javafx.scene.image.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Game extends Application {
Button start, battle, attack, defend;
Scene intro, beginBattle, standby;
MediaPlayer mediaPlayer;
int monsterHealth = 100;
int playerHealth = 100;
public void start(Stage primaryStage) {
primaryStage.setTitle("Dungeon Crawler"); // Gives the title to the window.
primaryStage.setResizable(false); // Prevents the window from being resized.
Rectangle rectDungeon = new Rectangle(75, 15, 650, 125); // creates a Rectangle object that consists of the following parameters: X, Y , WIDTH, HEIGHT.
rectDungeon.setStroke(Color.WHITE); // Colors the borders of the rectangle white.
//
// Media musicFile = new Media("file:Night%20Cave.mp3");
// mediaPlayer = new MediaPlayer(musicFile);
// mediaPlayer.setAutoPlay(true); // Sets the music to start as soon as the program runs. NOTE: TRYING TO GET A MP3 FILE IN AND WHENEVER I USE THIS IT CAUSES THE PROGRAM TO NOT RUN.
// NEED TO FIGURE SOMETHING OUT.
// Sets up the button that will be used
// to start the game when it runs.
start = new Button();
start.setText("Start");
start.setOnAction(e -> primaryStage.setScene(beginBattle)); // Sets the button to activate an event handler for the next scene (beast battle starts).
start.setLayoutX(360); // Sets the x axis location of the button.
start.setLayoutY(720); // Sets the y axis location of the button.
Image dungeonImage = new Image("file:Dungeon.png"); // Sets the image object as an image of a door.
ImageView dungeonView = new ImageView(); // Sets up an object of the ImageView class.
dungeonView.setImage(dungeonImage); // sets the image of the door to be viewed in the GUI
// Dialogue initializing the player to what their task is.
Text dungeonText = new Text(" You traverse the sewers of your city. "
+ "\n You heard of a monster who lurked here and took a quest to destroy it. "
+ "\n Worrying for the safety of the citizens you now search for it."
+ "\n Suddenly, you hear a growl and see a mass bellowing towards you.");
dungeonText.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20)); // Sets the text as Sans serif with no bolding, italics, and a size of 20
dungeonText.setStyle("-fx-fill: white;"); // Sets the text with the color white.
dungeonText.setLayoutX(90); // Sets the x axis location of the text.
dungeonText.setLayoutY(40); // Sets the y axis location of the text.
Pane root = new Pane(); // Sets an object to collect all items for the GUI for the scene in question.
root.getChildren().addAll(dungeonView, rectDungeon, dungeonText, start); // Collects the items needed in the scene.
intro = new Scene(root, 788, 788); // Sets up the GUI items, and the window size.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rectangle rectMonsterDungeon = new Rectangle(75, 15, 650, 65); // X, Y , WIDTH, HEIGHT ------------> CHANGE COMMENT
rectMonsterDungeon.setStroke(Color.WHITE);
// Sets up the button that will be used
// to move forward if in a one way hall.
battle = new Button();
battle.setText("Battle!");
battle.setOnAction(e -> primaryStage.setScene(standby)); // Sets the button to activate an event handler for the next scene (a hallway).
battle.setLayoutX(355); // Sets the x axis location of the button.
battle.setLayoutY(720); // Sets the y axis location of the button.
Image monsterDungeonImage = new Image("file:MonsterDungeon.png"); // Sets the image object as an image of a one way hallway.
ImageView monsterDungeonView = new ImageView(); // Sets up an object of the ImageView class.
monsterDungeonView.setImage(monsterDungeonImage); // sets the image of the one way hallway to be viewed in the GUI
// Dialogue warning the player of possible dangers
// ahead when they enter the first room of the crypt.
Text monsterDungeonText = new Text(" The beast has reared it's ugly head in front of you!"
+ "\n Prepare to battle or else perish!");
monsterDungeonText.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20)); // Sets the text as Sans serif with no bolding, italics, and a size of 20
monsterDungeonText.setStyle("-fx-fill: white;"); // Sets the text with the color white.
monsterDungeonText.setLayoutX(90); // Sets the x axis location of the text.
monsterDungeonText.setLayoutY(40); // Sets the y axis location of the text.
Pane root2 = new Pane(); // Sets an object to collect all items for the GUI for the scene in question.
root2.getChildren().addAll(monsterDungeonView, rectMonsterDungeon, monsterDungeonText, battle); // Collects the items needed in the scene.
beginBattle = new Scene(root2, 800, 800); // Sets up the GUI items, and the window size.
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
while(playerHealth > 0 && monsterHealth > 0) {
Rectangle rectMonsterDungeonStandby = new Rectangle(75, 15, 650, 65); // X, Y , WIDTH, HEIGHT ------------> CHANGE COMMENT
rectMonsterDungeonStandby.setStroke(Color.WHITE);
Rectangle rectPlayerBoxSB = new Rectangle(140 , 650, 500, 125);
rectPlayerBoxSB.setStroke(Color.WHITE);
// Sets up the button that will be used
// to move forward if in a one way hall.
attack = new Button();
attack.setText("Attack!");
attack.setOnAction(e -> primaryStage.setScene(intro)); // Sets the button to activate an event handler for the next scene (a hallway).
attack.setLayoutX(300); // Sets the x axis location of the button.
attack.setLayoutY(720); // Sets the y axis location of the button.
// Sets up the button that will be used
// to defend against the monster if the player
// wished to block all or most damage from the
// monster.
defend = new Button();
defend.setText("Defend!");
defend.setOnAction(e -> primaryStage.setScene(standby));
defend.setLayoutX(410);
defend.setLayoutY(720);
Image monsterDungeonImageSB = new Image("file:MonsterDungeonSTANDBY.png"); // Sets the image object as an image of a one way hallway.
ImageView monsterDungeonViewSB = new ImageView(); // Sets up an object of the ImageView class.
monsterDungeonViewSB.setImage(monsterDungeonImageSB); // sets the image of the one way hallway to be viewed in the GUI
// Dialogue warning the player of possible dangers
// ahead when they enter the first room of the crypt.
Text monsterDungeonTextSB = new Text(" Choose from the options below."
+ "\n The Beast's Health: " + monsterHealth);
monsterDungeonTextSB.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20)); // Sets the text as Sans serif with no bolding, italics, and a size of 20
monsterDungeonTextSB.setStyle("-fx-fill: white;"); // Sets the text with the color white.
monsterDungeonTextSB.setLayoutX(90); // Sets the x axis location of the text.
monsterDungeonTextSB.setLayoutY(40); // Sets the y axis location of the text.
Text playerTextSB = new Text("Which will you do?"
+ "\n Health: " + playerHealth);
playerTextSB.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20));
playerTextSB.setStyle("-fx-fill: white");
playerTextSB.setLayoutX(310);
playerTextSB.setLayoutY(685);
Pane root3 = new Pane(); // Sets an object to collect all items for the GUI for the scene in question.
root3.getChildren().addAll(monsterDungeonViewSB, rectMonsterDungeonStandby, rectPlayerBoxSB, monsterDungeonTextSB, playerTextSB, attack, defend); // Collects the items needed in the scene.
standby = new Scene(root3, 800, 800); // Sets up the GUI items, and the window size.
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
primaryStage.setScene(intro); // Sets the first scene in the GUI as the DOOR.png with its accompanying text and button.
primaryStage.show(); // Shows the GUI to the user and provides the different scenes in the program
// depending on the button choices the user makes.
}
public static void main(String[] args) {
launch(args);
}
}
由于 playerHealth 和 monsterHealth 最初都设置为大于 0,因此我希望 GUI 运行时就像不存在时间一样。
相反,我没有弹出 GUI 窗口,也没有出现错误。
【问题讨论】:
-
你可能需要这里的东西 -> docs.oracle.com/javase/8/javafx/api/javafx/animation/…
-
像大多数 UI 框架一样,JavaFX 是基于事件的。如果您在 JavaFX 应用程序线程中有一个长时间运行的循环或逻辑,JavaFX 将无法处理其事件,包括导致显示窗口的事件,以及允许窗口重绘自身和响应用户输入的事件。您根本无法使用循环来监控游戏进度。您必须将游戏状态保存在私有字段中,并使用事件侦听器或 ScheduledService 之类的东西定期检查该状态。
-
将 JavaFx 应用程序视为在单线程上运行的应用程序。当这个线程忙于运行 long while 循环时,它不会更新 gui。 gui 变得无响应(冻结)。
标签: java eclipse user-interface javafx while-loop