2017-05-31 3 views
0

Ich mache ein Text-basiertes Spiel mit Java, aber ich lief auf ein Problem.Ich bekomme immer eine Init-Ausnahme in meiner Klasse

Ich habe die erste Klasse „Game“ und die zweite Klasse „ForestEvents“ Ich versuche in der Spielklasse eine Methode (Stadttor) von der ForestEvents Klasse zu rufen, während auf Variablen aus der Spielklasse beziehen()

public class Game 
{ 
TitleScreenHandler tsHandler = new TitleScreenHandler(); 

ChoiceHandler choiceHandler = new ChoiceHandler(); 

ComponentHandler compHandler = new ComponentHandler(); 

ForestEvents fEvents = new ForestEvents(); 


JFrame window; 
Container con; 
JPanel titlePanel , startPanel, mainTextPanel, choiceButtonPanel, playerPanel; 
JLabel titleLabel, hpLabel, hpLabelNumber, weaponLabel, weaponLabelName; 
JButton startButton, choice1,choice2,choice3,choice4; 
JTextArea mainTextArea; 

Font titleFont = new Font("Times New Roman", Font.PLAIN, 100); 
Font normalFont = new Font("Times New Roman", Font.PLAIN, 30); 


String playerName; 
static String weapon,position; 

static int playerHP; 
int weaponDamage; 

public static void main(String[] args) 
{ 
    new Game(); 

} 

public Game() 
{ 
    window = new JFrame(); 
    window.setSize(1280,800); 
    window.setTitle("W: " + window.getWidth() + " H: " + window.getHeight()); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.getContentPane().setBackground(Color.black); 
    window.setLayout(null); 
    con = window.getContentPane(); 

    //Panels are used to make sections in the window (Background) 
    //Labels are used to write in the sections/panels (Foreground) 
    //Buttons can be pressed inside Panels 

    //To make a text you need to design a panel, design its size/color, 
    //Design its text the same way, then add it to the panel 

    titlePanel = new JPanel(); 
    titlePanel.setBounds(100, 100, 1080, 150); 
    titlePanel.setBackground(Color.black);  

    titleLabel = new JLabel("Adventure"); 
    titleLabel.setForeground(Color.white); 
    titleLabel.setFont(titleFont); 

    startPanel = new JPanel(); 
    startPanel.setBounds(540, 600, 200, 100); 
    startPanel.setBackground(Color.black); 

    startButton = new JButton("START"); 
    startButton.setBackground(Color.black); 
    startButton.setForeground(Color.white); 
    startButton.setFont(normalFont); 
    startButton.addActionListener(tsHandler); 

    titlePanel.add(titleLabel); 
    startPanel.add(startButton); 
    con.add(titlePanel); 
    con.add(startPanel); 

    window.setVisible(true); 

} 

public void createGameScreen() 
{ 
    titlePanel.setVisible(false); 
    startButton.setVisible(false); 

    mainTextPanel = new JPanel(); 
    mainTextPanel.setBounds(100, 100, 1080, 250); 
    mainTextPanel.setBackground(Color.black); 

    mainTextArea = new JTextArea("This is the main text area"); 
    mainTextArea.setBounds(100,100,1080,250); 
    mainTextArea.setBackground(Color.black); 
    mainTextArea.setForeground(Color.white); 
    mainTextArea.setFont(normalFont); 
    mainTextArea.setLineWrap(true); 

    playerPanel = new JPanel(); 
    playerPanel.setBounds(100, 20, 1080, 50); 
    playerPanel.setBackground(Color.blue); 
    playerPanel.setLayout(new GridLayout(1,4)); 

    choiceButtonPanel = new JPanel(); 
    choiceButtonPanel.setBounds(500, 350, 300, 250); 
    choiceButtonPanel.setLayout(new GridLayout(4,1)); 
    choiceButtonPanel.setBackground(Color.black); 

    choice1 = new JButton("Choice 1"); 
    choice1.setBackground(Color.black); 
    choice1.setForeground(Color.white); 
    choice1.setFont(normalFont); 
    choice1.setFocusPainted(false); 
    choice1.addActionListener(choiceHandler); 
    choice1.setActionCommand("c1"); 

    choiceButtonPanel.add(choice1); 


    choice2 = new JButton("Choice 2"); 
    choice2.setBackground(Color.black); 
    choice2.setForeground(Color.white); 
    choice2.setFont(normalFont); 
    choice2.setFocusPainted(false); 
    choice2.addActionListener(choiceHandler); 
    choice2.setActionCommand("c2"); 

    choiceButtonPanel.add(choice2); 


    choice3 = new JButton("Choice 3"); 
    choice3.setBackground(Color.black); 
    choice3.setForeground(Color.white); 
    choice3.setFont(normalFont); 
    choice3.setFocusPainted(false); 
    choice3.addActionListener(choiceHandler); 
    choice3.setActionCommand("c3"); 

    choiceButtonPanel.add(choice3); 


    choice4 = new JButton("Choice 4"); 
    choice4.setBackground(Color.black); 
    choice4.setForeground(Color.white); 
    choice4.setFont(normalFont); 
    choice4.setFocusPainted(false); 
    choice4.addActionListener(choiceHandler); 
    choice4.setActionCommand("c4"); 

    choiceButtonPanel.add(choice4); 


    con.add(mainTextPanel); 
    con.add(choiceButtonPanel); 
    con.add(playerPanel); 

    hpLabel = new JLabel("HP: "); 
    hpLabel.setFont(normalFont); 
    hpLabel.setForeground(Color.white); 

    hpLabelNumber = new JLabel(); 
    hpLabelNumber.setFont(normalFont); 
    hpLabelNumber.setForeground(Color.white); 

    weaponLabel = new JLabel("Weapon: "); 
    weaponLabel.setFont(normalFont); 
    weaponLabel.setForeground(Color.white); 

    weaponLabelName = new JLabel(); 
    weaponLabelName.setFont(normalFont); 
    weaponLabelName.setForeground(Color.white); 


    playerPanel.add(hpLabel); 

    playerPanel.add(hpLabelNumber); 

    playerPanel.add(weaponLabel); 

    playerPanel.add(weaponLabelName); 

    mainTextPanel.add(mainTextArea); 

    playerSetup(); 

} 

public void playerSetup() 
{ 
    playerHP = 15; 
    weapon = "Fists"; 
    weaponLabelName.setText(weapon); 
    hpLabelNumber.setText("" + playerHP); 

    fEvents.townGate(); 
} 

public void townGate() 
{ 
    position = "towngate"; 

    mainTextArea.setText("You are at the gates of the town. A guard is standing in front of you. What do you do?"); 
    choice1.setText("Talk to the Guard"); 
    choice2.setText("Attack the Guard"); 
    choice3.setText("Leave"); 
    choice4.setText(""); 
} 


public void talkGuard() 
{ 
    position = "talkguard"; 

    mainTextArea.setText("Guard: Hello Stranger. I have never seen you before. I'm sorry but I cannot let you enter."); 

    choice1.setText("Go Back"); 
    choice2.setText(""); 
    choice3.setText(""); 
    choice4.setText(""); 

} 

public void attackGuard() 
{ 
    position = "attackguard"; 

    mainTextArea.setText("Guard: HOW DARE YOU! \nThe guard fought back and hit you hard.\n(You received 3 damage)"); 
    playerHP -= 3; 
    hpLabelNumber.setText("" + playerHP); 

    choice1.setText("Go Back"); 
    choice2.setText(""); 
    choice3.setText(""); 
    choice4.setText(""); 

} 

public void crossRoad() 
{ 
    position = "crossroads"; 

    mainTextArea.setText("You are at the crossroad.\n Go south to go back to the town."); 

    choice1.setText("Go North"); 
    choice2.setText("Go East"); 
    choice3.setText("Go South"); 
    choice4.setText("Go West"); 

} 

public class ComponentHandler implements ComponentListener 
{ 
    public void componentResized(ComponentEvent e) 
    { 
     Component c = (Component)e.getSource(); 
     window.setTitle("W: " + c.getWidth() + " H: " + c.getHeight()); 
    } 

    @Override 
    public void componentHidden(ComponentEvent e) 
    { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void componentMoved(ComponentEvent e) 
    { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void componentShown(ComponentEvent e) 
    { 
     // TODO Auto-generated method stub 

    } 
} 


public class TitleScreenHandler implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     createGameScreen(); 
    } 
} 

public class ChoiceHandler implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     String yourChoice = event.getActionCommand(); 

     switch (position) 
     { 
      case "towngate": 
       switch(yourChoice) 
       { 
       case "c1": 
        talkGuard(); 
        break; 
       case "c2": 
        attackGuard(); 
        break; 
       case "c3": 
        crossRoad(); 
        break; 
       case "c4": 
        break; 
       } 
       break; 

      case "talkguard": 
       switch(yourChoice) 
       { 
       case "c1": 
        fEvents.townGate(); 
        break; 
       } 
       break; 

      case "attackguard": 
       switch(yourChoice) 
       { 
       case "c1": 
        fEvents.townGate(); 
        break; 
       } 
       break; 

      case "crossroad": 
       switch(yourChoice) 
       { 
       case"c1": 
        break; 
       case"c2": 
        break; 
       case"c3": 
        fEvents.townGate(); 
        break; 
       case"c4": 
        break; 
       } 
       break; 


     } 

    } 

} 
} 

die zweite Klasse

public class ForestEvents extends Game 
{ 

public void townGate() 
{ 
    position = "towngate"; 

    mainTextArea.setText("You are at the gates of the town. A guard is standing in front of you. What do you do?"); 
    choice1.setText("Talk to the Guard"); 
    choice2.setText("Attack the Guard"); 
    choice3.setText("Leave"); 
    choice4.setText(""); 
} 
} 

Aber immer, wenn ich das Programm laufen bekomme ich eine Ausnahme, die sagt:

Exception in thread "main" java.lang.StackOverflowError

bei ForestEvents. (ForestEvents.java:2)

am Spiel. (Game.java:23)

Was ich bin Versuchen zu tun ist die Methoden in der 1. Klasse zu nennen und sie in der 2md Klasse zu machen, also wenn ich mehr Methoden in der 2. Klasse hinzufüge, werden sie leichter zu verwalten sein als alles in der 1. Klasse.

Meine Frage ist, wie repariere ich die Ausnahme? Oder gibt es einen effizienteren Weg, dies zu tun?

Antwort

1

Ihre ForestEvents extends Game - so alle ForestEvent s wird intern eine Instanz von ForestEvent s haben, eine Instanz von ForestEvent s ..

Das ist, was die Stackoverflow-Fehler verursacht.

Ich schlage vor, Sie Feld ForestEvents loswerden und verwenden Sie es als lokale Variable in main() stattdessen.

Es ist ein gültiger Ansatz, um Klassen ordentlich zu halten - aber in diesem Fall möchten Sie vielleicht eine "Starter" -Klasse erstellen (nicht Game selbst) und den Hauptteil in das schreiben.

Verwandte Themen