1

I would like to apologize in advance I am new to java and don't know very much so stick with me please. I would like to know how to get my character attributes from one ActionListener method in one class to another ActionListener method in another class. I would like to get the inputs from the user about player1 in one class and then use them in the other class. Please help, and I appreciate your time.

public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub

    if(event.getSource() == create){

        Character player1 = new Character( Integer.parseInt(strength.getText()), Integer.parseInt(defense.getText()), Integer.parseInt(health.getText()) , Integer.parseInt(dexterity.getText()));
        player1.name = name.getText();

        JOptionPane.showMessageDialog(this, "\nName: " + player1.name + "\nHealth: " + player1.health + "\nStrength: " + player1.strength + "\nDefense: " + player1.defense + "\nDexterity: " + player1.dexterity);
        dispose();//To close the current window

       GameWindow gwindow = new GameWindow();
        gwindow.setVisible(true);//Open the new window
    }

put into

@Override
public void actionPerformed(ActionEvent event) {

    Object source = event.getSource();
    Character Goblin = new Character(10, 3, 6, 10);
    Character Witch = new Character(2, 7, 3, 20);
    Character Zombie = new Character(5, 5, 5, 15);
    int damage;

    if (event.getSource() == right) {

        label1.setText("You have encountered a goblin!");
        label2.setText("Do you wish to fight or flee?");
        fight.setVisible(true);
        flee.setVisible(true);
    }

        if(event.getSource() == fight) {

            System.out.println(player1 + " VS. " + Goblin.name);

            while(player1.isAlive() && Goblin.isAlive()){

                // player 1 attack
                damage = player1.attack(Goblin);
                System.out.println(player1.name + " hits " + Goblin.name + " for " + damage + " damage.");

                // player 2 attack
                damage = Goblin.attack(player1);
                System.out.println(Goblin.name + " hits " + player1.name + " for " + damage + " damage.\n");
            }

            // Check to see who won
            if( player1.isAlive()){
                System.out.println(player1.name + " wins!");
            }
            else{
                System.out.println("You have perished");
            }

        }
}
kdl
  • 55
  • 6

2 Answers2

0

Declare Player1 as public Static member So it's Value can;t be changed.

and You can use player1 Through the object of that particular Class.

    Class First{
          //Declare That Character object as a static public here
          //Player1;
           }
    Class Second{
          //First Create Object Of that class....
          First f = new First(//Parameter For Constructor);
          f.Player1;

           }
Vk Suhagiya
  • 343
  • 3
  • 11
0

Change your GameWindow constructor like this.

class GameWindow extends JFrame implements ActionListener{
    private Character player1;

    public GameWindow(Character player1){
        this.player1 = player1;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        Character Goblin = new Character(10, 3, 6, 10);
        Character Witch = new Character(2, 7, 3, 20);
        Character Zombie = new Character(5, 5, 5, 15);
        int damage;

        if (event.getSource() == right) {

            label1.setText("You have encountered a goblin!");
            label2.setText("Do you wish to fight or flee?");
            fight.setVisible(true);
            flee.setVisible(true);
        }

        if(event.getSource() == fight) {

            System.out.println(player1 + " VS. " + Goblin.name);

            while(player1.isAlive() && Goblin.isAlive()){

                // player 1 attack
                damage = player1.attack(Goblin);
                System.out.println(player1.name + " hits " + Goblin.name + " for " + damage + " damage.");

                // player 2 attack
                damage = Goblin.attack(player1);
                System.out.println(Goblin.name + " hits " + player1.name + " for " + damage + " damage.\n");
            }

            // Check to see who won
            if( player1.isAlive()){
                System.out.println(player1.name + " wins!");
            }
            else{
                System.out.println("You have perished");
            }

        }
    }
}

And pass parameter to new contructor.

public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub

    if(event.getSource() == create){

        Character player1 = new Character( Integer.parseInt(strength.getText()), Integer.parseInt(defense.getText()), Integer.parseInt(health.getText()) , Integer.parseInt(dexterity.getText()));
        player1.name = name.getText();

        JOptionPane.showMessageDialog(this, "\nName: " + player1.name + "\nHealth: " + player1.health + "\nStrength: " + player1.strength + "\nDefense: " + player1.defense + "\nDexterity: " + player1.dexterity);
        dispose();//To close the current window

       GameWindow gwindow = new GameWindow(player1);
        gwindow.setVisible(true);//Open the new window
    }
thangdc94
  • 1,542
  • 2
  • 26
  • 38