I'm currently messing around trying to make a game in Java. Currently, I'm working across three CLasses Main, MyFrame, and Player.
public class Player {
double FIVE_PERCENT = .05;
double TEN_PERCENT = .1;
double maxHP;
double currHP;
double maxStam;
double currStam;
double stamRegen = (TEN_PERCENT * maxStam) ;
double hpRegen = TEN_PERCENT * maxHP;
int xpToLvl;
int currXP;
int currLvl;
Player(double _maxHP, double _currHP, double _maxStam, double _currStam, int _xpToLvl, int _currXP, int _currLvl) {
maxHP = _maxHP;
currHP = _currHP;
maxStam = _maxStam;
currStam = _currStam;
xpToLvl = _xpToLvl;
currXP = _currXP;
currLvl = _currLvl;
}
public double RegenHP() {
while(currHP < maxHP){
try {
currHP += maxHP * TEN_PERCENT;
if (currHP > maxHP) {currHP = maxHP;}
System.out.println(currHP + ": Health Points");
Thread.sleep(500);
} catch (Exception e) {
System.out.println("uh oh stinky");
}
}
return currHP;
}
public double RegenStam() {
while(currStam < maxStam){
try {
currStam += maxStam * TEN_PERCENT;
if (currStam > maxStam) {currStam = maxStam;}
System.out.println(currStam + ": Stamina Points");
Thread.sleep(500);
} catch (Exception e) {
System.out.println("uh oh stinky");
}
}
return currStam;
}
public int CheckLevelUp() {
if(currXP >= xpToLvl){
xpToLvl *= 1.2;
currLvl++;
System.out.println("Current Level: " + currLvl);
System.out.println("XP to next level: " + xpToLvl);
}
return currLvl;
}
public int AddXP() {
System.out.println("Initial xp: " + currXP);
currXP++;
System.out.println("Post xp: " + currXP);
CheckLevelUp();
return currXP;
}
}
My issue is with the AddXP(); I had wanted to call the method on the _player1 object initialized in Main, but the issue is more with how. I wanted to add a JButton in the MyFrame class, but MyFrame can't see _player1 nor can it recognize AddXP();
public class Main {
public static void main(String[] args) {
new MyFrame();
Player _player1 = new Player(40, 40, 50, 50, 24, 23, 1);
_player1.RegenStam();
_player1.RegenHP();
_player1.AddXP();
}
}
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame{
Player _player1;
JButton xpButton;
int SCREEN_WIDTH = 800;
int SCREEN_HEIGHT = 600;
MyFrame(){
xpButton = new JButton();
xpButton.setBounds( 0, 0, 20, 20);
xpButton.setVisible(true);
xpButton.addActionListener((e) -> _player1.AddXP());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(new Color(200, 200, 215));
this.setBounds(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
this.add(xpButton);
}
}