Jump to content
  • Advertisement
Sign in to follow this  

Creating object in external class

This topic is 2414 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() {

public void initGUI {

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

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

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

class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e){
frame.add(new Barracks());

class DaggerAction implements ActionListener {
public void actionPerformed(ActionEvent e){
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!