• Advertisement
Sign in to follow this  

Creating object in external class

This topic is 2144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am creating Java game, it is my first game, so I am still a noob in that case. I got stuck in one thing:
I have created main menu, after selecting start new game, user must select weapon (ex. dagger with JButton), this is how it looks:


public class GUI {

public GUI() {
initGUI();
}

public void initGUI {

(..................some methods.........................)

final JButton newGame = new JButton(new ImageIcon(..));
newGame.setLocation(250, 420);
content.add(newGame);

final JButton buttonDagger = new JButton(new ImageIcon(..));
buttonDagger.setLocation(250, 420);
content.add(buttonDagger);

class NewGameAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
newGame.setVisible(false);
daggerButton.setVisible(true);
frame.add(new Barracks());
}
}


class DaggerAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
daggerButton.setVisible(false);
Weapon w = new Weapon("Dagger", 3, 8, 5);
frame.add(new Arena());
}
}
buttonDagger.addActionListener(new DaggerAction());
newGame.addActionListener(new NewGameAction());
}
}


Class DaggerAction is inside initGUI function, is an initialization of daggerButton, after clicking on that, dagger must be created. So my problem is, how I can reach that Weapon w object? Or maybe there is an option to create it's copy somewhere during DaggerAction initialization? Or maybe there is an other way to initialize such select thigs? I will really appreciate your help, thanks. GUI is the main class which handle all the graphics. And I want to appear weapon object in Barracks class, which is initialized after starting new game.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement