如何在多个类java

时间:2017-11-02 06:17:37

标签: java class object javafx getter-setter

我正在使用java fxfxml控制器在java中制作一个相当大的基于文本的游戏来表示游戏中的每个位置。

我有一个player类,代表我的数据库中的玩家统计数据表。 player需要先与Jarl交谈才能在北方森林中完成任务。

所以我在我的播放器类中使用了一个级别计数器整数,当用户保存游戏时,它可以存储在数据库中。在说出jarl之后,玩家进入门卫,门卫确定天气,玩家准备完成任务。当我尝试点击“去北方森林”。它会返回0或在这种情况下我得到NullPointerException

我知道我无法在门控制器中创建player类的新实例,因为它只是默认为零,但是如何在我的播放器类中使用允许{{{}的通用方法1}}用于多个类?

我尝试使用构造函数设置,但这也不起作用。我使用setters/getters检查了我的假设,它仍然是system.out.println

  

玩家类。

returns 0
  

JarlsofficeController类

public class Player {

int health = 100, attackdamage = 100, armor = 0, bank = 100, Game_ID = 0, Jarl_level_count = 0;

/**
 *
 * @param Name
 * @param Description
 * @param Cost
 * @param health
 */
public Player() {
    this.Game_ID = 0;
    this.Jarl_level_count = 0;
    this.armor = 0;

    this.attackdamage = 0;
    this.bank = 0;
    this.health = 0;
}

public Player(int Game_ID, int Jarl, int attackdamage, int armor, int bank, int health) {

    this.Game_ID = Game_ID;
    this.Jarl_level_count = Jarl;
    this.armor = armor;

    this.bank = bank;
    this.attackdamage = attackdamage;
    this.health = health;
}

public void update(Player thisThing) {
    health = thisThing.health;
    bank = thisThing.bank;
    attackdamage = thisThing.attackdamage;
    armor = thisThing.armor;
    Jarl_level_count = thisThing.Jarl_level_count;

}

public int getLevel() {
    return this.Jarl_level_count;
}

public void setLevel(int level) {
    this.Jarl_level_count = level;
}

@Override
public String toString() {
    String outputString;
    outputString = Jarl_level_count + "\n";

    return outputString;

}
}
  

GateController类

public class JarlsofficeController implements Initializable {

Player you;
@FXML
TextArea prompt;
@FXML

Button Bill_rates;
@FXML
Button Ryan_hellgon;
@FXML
Button Victoria_dove;
@FXML
Button Game_Room;
@FXML
Button Exit;

public JarlsofficeController() {
    this.prompt = new TextArea();
    this.Bill_rates = new Button();
    this.Ryan_hellgon = new Button();
    this.Victoria_dove = new Button();
    this.Game_Room = new Button();

    this.Exit = new Button();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    prompt.setPrefSize(850, 50);
    you = new Player(0, 0, 0, 0, 0, 0);
    //prompt.setText(intro);
    prompt.setEditable(false);
}

  public void Ryan() {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setContentText("Jarl: Ahhhhhh..... adventurer what brings you here today? ");
    ButtonType yes = new ButtonType("I heard you may have some work?");
    ButtonType no = new ButtonType("Adventurer?");
    ButtonType Cancle = new ButtonType("Cancle");

    alert.getButtonTypes().setAll(yes, no, Cancle);
    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == yes) {
        alert.setHeight(600);
        alert.setContentText("Jarl: Ahhh yes I do. Long ago there was a proficy that an adventure would come to Davenrun and save Taledone from the forces of evil. A man who sets out to save what Davenrun was founded on… a haven of adventures like your self. \n Taladone is becoming dangours. I don’t have guards to spare the town. That is where you come in adventure. There are diffrent places among taladone that need to be explored. If you dare take upon the quest.");

        ButtonType yes2 = new ButtonType("What must I do?");
        ButtonType no2 = new ButtonType("That sounds dangourous?");

        alert.getButtonTypes().setAll(yes2, no2);
        Optional<ButtonType> result2 = alert.showAndWait();
        if (result2.get() == yes2) {
            alert.setHeight(600);
            alert.setContentText("Jarl: Go the Taladone Forrest for a start. There are mystroues creatures as well as a dungon that has an evil dragon that is ruling North Taladone.\n I’m afraid the dragon is going to leave the cave and attack the city we will all be doomed. Adventure. \n Go the forrest kill all the evil that haunts it and kill the dragon. If you can do that, I will pay you dearly.");

            ButtonType yes3 = new ButtonType("Yes");
            ButtonType no3 = new ButtonType("No");

            alert.getButtonTypes().setAll(yes3, no3);
            Optional<ButtonType> result3 = alert.showAndWait();

            if (result3.get() == yes3) {
                alert.setHeight(600);
                alert.setContentText("Jarl: Go to the gate and start your adventure!");

                you = new Player(0, 1, 0, 0, 0, 0);

                System.out.println(you.getLevel());

                ButtonType ok = new ButtonType("OK");
                alert.getButtonTypes().setAll(ok);
                Optional<ButtonType> new_result = alert.showAndWait();
            }
            if (result3.get() == no3) {
                alert.setHeight(600);
                alert.setContentText("Jarl: I am here if you change your mind...");
                ButtonType ok = new ButtonType("OK");
                alert.getButtonTypes().setAll(ok);
                Optional<ButtonType> new_result = alert.showAndWait();
            }
        }
        if (result2.get() == no2) {
            alert.setHeight(600);
            alert.setContentText("As is the rest of taladone it is I'm afraid. When I was a young boy my father took me on a boat and traveled the Irish sea to this Island, some sort of evil came and made the island worse and worse. \n The creates became increasly difficult and dangours. Now Taladone is an island of evil and we are in the center of it. I fear if noone stops it soon, we are all going to die....");
            ButtonType ok = new ButtonType("OK");
            alert.getButtonTypes().setAll(ok);
            Optional<ButtonType> new_result = alert.showAndWait();

        }

    }
    if (result.get() == no) {
        alert.setHeight(600);
        alert.setContentText("An adventure. A one who comes to the island to fight the forces of evil. \n A man who is wise and has no fear. A man who is brave and bold at the same time. A man who isn’t doesn’t fear death, they welcome.\n You have those qualities to save Taladone and the rest of the world.");
        ButtonType ok = new ButtonType("OK");
        alert.getButtonTypes().setAll(ok);
        result = alert.showAndWait();

    }
    if (result.get() == Cancle) {
        alert.setHeight(600);
        alert.setContentText("Well Alright then!");
        ButtonType ok = new ButtonType("OK");
        alert.getButtonTypes().setAll(ok);
        Optional<ButtonType> new_result = alert.showAndWait();
    }
    alert.getButtonTypes().setAll(yes, no, Cancle);
    alert.showAndWait();
}

public void exit() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("/Davenrun/Inside_Jarls_palace.fxml"));
    Stage Stage = new Stage();
    Stage.setTitle("jarls palace");
    Stage.setScene(new Scene(root, 1500, 200));

    Stage.show();
    closeWindow();
}

@FXML
public void closeWindow() {
    Stage stage = (Stage) this.Exit.getScene().getWindow();
    stage.close();
}

}

这是fxml类,虽然除了编译之外不需要任何其他原因。忽略onAction错误,这些只是我没有在这里发布的方法。

public class GateController implements Initializable {

@FXML
private TextArea prompt;
@FXML
private Button Northen_woods;
@FXML
private Button Eastern_Graveyard;
@FXML
private Button Southern_ocean;
@FXML
private Button Western_Moutnain;
@FXML
private Button Go_North;
@FXML
private Button Go_NorthWest;

public GateController() {
    this.prompt = new TextArea();
    this.Northen_woods = new Button();
    this.Eastern_Graveyard = new Button();
    this.Southern_ocean = new Button();
    this.Western_Moutnain = new Button();
    this.Go_North = new Button();
    this.Go_NorthWest = new Button();
}

public void Northen_woods() throws IOException {
    JarlsofficeController you = new JarlsofficeController();
    System.out.println(you.you.getLevel());
    if (you.you.getLevel() == 0) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Gate Guard");
        alert.setContentText("Gate Guard: You are not ready. I suggest you go talk to the Jalr first. ");

        ButtonType ok = new ButtonType("OK");
        alert.getButtonTypes().setAll(ok);
        alert.showAndWait();
    }
    if (you.you.getLevel() == 1) {

        Parent root = FXMLLoader.load(getClass().getResource("/The_northen_woods/Northen_bridge.fxml"));

        Stage stage = new Stage();

//        stage.setTitle("Outside house");
        stage.setScene(new Scene(root, 500, 500));
        stage.show();
        closeWindow();
      }

      }
    @FXML
  public void closeWindow() {
    Stage stage = (Stage) this.Eastern_Graveyard.getScene().getWindow();
    stage.close();
  }

最后,这是jarls办公室的fxml     

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

 <?import java.lang.*?>
 <?import java.net.*?>
 <?import java.util.*?>
 <?import javafx.scene.*?>
 <?import javafx.scene.control.*?>
 <?import javafx.scene.layout.*?>

 <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
 styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" 
  fx:controller="Davenrun.GateController">
  <children>
    <TextArea fx:id="prompt" layoutX="100" layoutY="0.0" />
    <Button fx:id="Northen_woods" layoutX="100" layoutY="50.0" 
 mnemonicParsing="false" text="Go to Northen Woods" 
  onAction="#Northen_woods"  />
    <Button fx:id="Eastern_Graveyard" layoutX="100" layoutY="75.0" 
 mnemonicParsing="false" text="Go to Eastern Graveyard"  onAction = 
 "#Eastern_Graveyard"/>
    <Button fx:id="Southern_ocean" layoutX="100" layoutY="100.0" 
mnemonicParsing="false" text="Go to Southern_Ocean" 
onAction="#Southern_Ocean"  
/>       
    <Button fx:id="Western_Moutnain" layoutX="100" layoutY="125.0" 
 mnemonicParsing="false" text="Go to Western Mountain"  
 onAction="#Western_Montain"/>
    <Button fx:id = "Go_North" layoutX="100" layoutY="150.0" 
  mnemonicParsing="false" text="Go North" onAction ="#go_north"/> 
    <Button fx:id="Go_NorthWest" layoutX="100" layoutY="175.0" 
  mnemonicParsing="false" text="Go NorthWest" onAction ="#go_northWest"/>

  </children>
</AnchorPane>

一个可能的解决方案将是伟大的,我知道我可以使用SQL来解决这个问题,但我正在尝试使这个软件“Codecentric”而不是“Datacentric”,并且只在我保存和加载时才使用sql游戏,因为我希望这个软件有一些java主题/概念。我知道这个方法在jarl方法中存在一些逻辑错误,软件仍在开发中,我现在正试图弄清楚骨头。谢谢你的帮助。 <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Davenrun.JarlsofficeController"> <children> <TextArea fx:id="prompt" layoutX="100" layoutY="0.0" text = "Welcome to the jarls office"/> <Button fx:id="Bill_rates" layoutX="100" layoutY="100.0" mnemonicParsing="false" text="Talk to Davenrun Guard" onAction = "#Bill" /> <Button fx:id="Ryan_hellgon" layoutX="100" layoutY="150.0" mnemonicParsing="false" text="Talk the Jarl of Davenrun" onAction = "#Ryan"/> <Button fx:id="Exit" layoutX="100" layoutY="200.0" mnemonicParsing="false" text="Exit" onAction = "#exit"/> </children> </AnchorPane>

0 个答案:

没有答案