【发布时间】:2016-05-16 17:44:59
【问题描述】:
我一直在为我的 AP Java 课程做一个小型冒险游戏作为最后一个项目,我一直在尝试实现我为 JavaFX GUI 制作的类使用Scene Builder 构建。
但由于某种原因,GUI 拒绝识别我在 Adventure 类中的方法 startAdventure(),它基本上启动了整个过程。 GUI 已启动,但无法正确显示 startAdventure() 中告知的内容。
startAdventure() 似乎直到 GUI 关闭后才被调用,导致空指针异常,因为 startAdventure() 应该在 GUI 中操作文本区域等。
如果我没有提供足够的信息,不理解建议等,我提前道歉。在编码方面我有点挑战,而且我对在这个网站上发布东西完全陌生。
这是 startAdventure() 的开头在我的 Adventure 类中的样子(text 和 inputText 是我的 AdventureGUI.fxml 文件中正确初始化的变量,它是使用 Scene Builder 创建的):
public void startAdventure()
{
//welcome user
text.setText("Welcome to the land of euphoria!" + "\n");
//gets users name
text.appendText("What's your name?" + "\n");
p.playerName = inputText.getText();
text.appendText("Welcome, " + p.playerName + "\n");
//sets start location
p.setNewLoc("START");
这是我的主要课程的外观:
public class RunAdventure extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("AdventureGUI.fxml"));
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.setTitle("Adventure");
primaryStage.show();
}
public static void main(String[] args)
{
Adventure game = new Adventure();
launch(args);
game.startAdventure();
}
} //end class
如果工作正常,GUI 将启动并在文本区域内显示以“欢迎来到欣快之地!”开头的文本。
但是,在当前状态下,GUI 已启动,并且在文本区域中根本没有文本。
再一次,这里的一切都是新的,所以如果你认为还有其他需要我展示的东西来帮助建议过程,请立即告诉我。
编辑:当我尝试更改 game.startAdventure() 的顺序时,将它放在 start 方法中的 primaryStage.show() 之前,出现了一长串异常。 这是我所做的更改:
@Override
public void start(Stage primaryStage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("AdventureGUI.fxml"));
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.setTitle("Adventure");
Adventure game = new Adventure();
game.startAdventure();
primaryStage.show();
}
以下是发生的异常情况:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at Classwork.FinalExamProject.Adventure.startAdventure(Adventure.java:102)
at Classwork.FinalExamProject.RunAdventure.start(RunAdventure.java:24)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
我还应该提到 Adventure 类也可以用作控制器
编辑 (5/20):好的,我的程序中的大部分内容都已完成,但我现在遇到了一个新问题。在我的冒险中,我有一个功能可以让玩家在给定位置与各种怪物战斗。我的问题是,一旦我到达程序中的这个位置,程序就会停止响应。在进行了一些调试之后,我发现这是一个 while 循环,我在 doBattle() 方法中执行了 player.isAlive 和enemy.isAlive 中的所有操作。唯一的问题是,一旦我删除了 while 循环,程序就会直接跳转到怪物死亡或玩家死亡之后,而它应该让你选择是否重复攻击怪物,直到任一实体是死的。我知道我在这个问题上塞进了太多东西,但我不知道还能把它放在哪里,到目前为止,我已经得到了这里每个人的压倒性支持。我不能感谢你们,但我不知道如何自己解决这个问题。 doBattle() 方法的代码如下:
public void doBattle(Monster enemy)
{
//boolean fled = false;
//Greets player into battle by telling what monster they are fighting
text.appendText("\n" + "A wild " + enemy.getName() + " has appeared!" + "\n" );
mobImagePane.setImage(enemy.getImage());
//System.out.print("THIS RAN"); //debug
while ( p.getHealth() > 0 && enemy.getHealth() > 0 ) //while enemy and player are alive
{
//Prompts user to attack or run
text.appendText("Attack " + enemy.getName() + "? (Y/N) " + "\n");
inputText.setOnAction(event ->
{
String fightChoice = inputText.getText();
fightChoice = fightChoice.toUpperCase();
//String fightChoice = this.choice;
if (fightChoice.equals("Y"))//if they want to fight
{
//Player turn
enemy.setHealth(enemy.getHealth() - p.getDamage()); //Sets the monsters health as their current health minus the players damage
text.appendText("You attack " + enemy.getName() + " for " + p.getDamage() + " damage!" + "\n" + "Enemy health is " + enemy.getHealth() + "\n");
//Monster turn
p.setHealth(p.getHealth() - enemy.getDamage()); //Sets the players health as their current health minus the monsters damage
text.appendText("The " + enemy.getName() + " hit you for " + enemy.getDamage() + " damage!" + "\n" + "Your health is " + p.getHealth() + "\n"); //prints how much damage the monster does to the player
if (p.health < 20.0) {
text.appendText("Your health is low, you should return home and restore health!" + "\n");
}
//checks if the player or monster is dead
this.checkLife();
enemy.checkLife();
} else {
if (fightChoice.equals("N")) // if they don't want to fight
{
mobImagePane.setImage(null);
text.appendText("You fled from the fight!" + "\n");
this.setNewLoc("TOWN"); // brings you back to town
fled = true;
//JOptionPane.showMessageDialog(null, "You are now in " + this.currentLoc.getName() + "\n" + this.currentLoc.getDescription());
//String move2 = JOptionPane.showInputDialog(null,"Which way do you want to go? (N, E, S, W)");
//makeMove(move2);
//break;
} else // they don't make any sense
{
text.appendText("Unrecognized command" + "\n");
}
}
//}
//when someone dies
if (!fled) {
if (p.alive) // if you are still standing
{
//print results (money earned, health remaining)
mobImagePane.setImage(null);
p.wallet += enemy.getLoot();
playerInfo.setText(p.getPlayerName() + "\n" + "Health: " + p.getHealth() + "\n" + "Wallet: " + p.getWallet() + "\n");
text.setText("You shrekt the " + enemy.getName() + "\n" + "You got $" + enemy.getLoot() + " for winning!" + "\n" + "You now have $" + p.wallet + "\nYour health is " + p.getHealth() + "\n");
} else //if you died
{
mobImagePane.setImage(null);
text.setText("You have been shrekt by the " + enemy.getName() + "\n" + "GAME OVER" + "\n");
text.appendText("\nPlay again? (Y/N)" + "\n");
inputText.setOnAction(event2 -> {
String answer = inputText.getText();
answer.toUpperCase();
//String answer = this.choice;
if (answer.equals("Y")) //if they want to play again
{
text.appendText("Alright! Let's go!" + "\n");
this.reset();
} else //if they want to quit
{
text.appendText("Wow. What a skrub, okay bye." + "\n");
System.out.close();
}
});
}
}
});
}
} //end doBattle
【问题讨论】:
-
你试过将
game.startAdventure();指令放在launch(args);之前吗?
标签: java user-interface javafx scenebuilder