I’m making a Java game that has a main class, a battle class, a monster class, a player class, etc… I have stats displayed on the main class frame. A button press creates a new battle frame, which also shows stats. After the battle, the stats are different (for example, the Player’s health points are lower than they were before being hit by the monster). I need these changes in the stats to be reflected back on the main frame after the battle frame disappears.
What is the best way to tell the main class that the battle is finished and to refresh the stats? Code is below. It’s a work in progress, obviously, and there are a few other irrelevant classes.
portion of main class that starts a battle (ie, creates a new battle object, which is a JFrame):
public class DarkWorld extends JFrame implements ActionListener
{
...
public DarkWorld()
{
...
public void createInfoLabel()
{
infoLabel.setText("<html>Player: " + DarkWorld.user.getPlayerName()
+ "<br>Health: " + DarkWorld.user.getHealth()
+ "<br>Attack Damage: " + DarkWorld.user.getAttackDamage()
+ "<br>Level: " + DarkWorld.user.getLevel()
+ "<br>Experience: " + DarkWorld.user.getExperience());
}
...
if(source == monsterButton)
{
BattleFrame battle = new BattleFrame();
final int WIDTH = 700;
final int HEIGHT = 500;
battle.setSize(WIDTH, HEIGHT);
battle.setVisible(true);
}
...
}
Portion of Battle class:
...
public class BattleFrame extends JFrame implements ActionListener
{
Monster newMonster = new Monster();
...
public BattleFrame()
...
public void displayStats()
{
monsterLabel.setText("<html>Type: " + newMonster.getMonsterName()
+ "<br>Health: " + newMonster.getHealth()
+ "<br>Attack: " + newMonster.getAttackName()
+ "<br>Attack Damage: " + newMonster.getAttackDamage());
playerLabel.setText("<html>Player: " + DarkWorld.user.getPlayerName()
+ "<br>Health: " + DarkWorld.user.getHealth()
+ "<br>Attack Damage: " + DarkWorld.user.getAttackDamage()
+ "<br>Level: " + DarkWorld.user.getLevel()
+ "<br>Experience: " + DarkWorld.user.getExperience());
}
...
if(newMonster.getHealth() <= 0)
{
...
JOptionPane.showMessageDialog(null, "You killed the monster! You found a " + newItem.getItemName()
+ ". It has been added to your inventory.");
dispose();
}
if(DarkWorld.user.getHealth() <= 0)
{
//if player dies (health reaches zero), the game is over - need to make game end
displayStats();
JOptionPane.showMessageDialog(null, "The monster killed you!");
dispose();
}
}
...
}
EDIT: Tried to simplify the code here so it is more clear what I am asking.
Why don’t you just create a method in your main class:
Then, in your Battle class, when the battle is done, you just call that method. Also, I think you might find it useful to implement the Singleton pattern in your project, since it sounds like there will only be one battle class at all times.
You might also want to put all of the battle logic into a class. The best programming advice I’ve ever gotten was to seperate logic from GUI.
Or, you might just want to ignore everything I’ve said since I’m only 13 and I program in C#, not Java.