Skip to main content
GameDev.net gamedev.net
🔒 Locked

Creating object in external class

Started by NuLogic Apr 11, 2012 at 2:02 PM 0 replies 900+ views
Original Post
NuLogic
NuLogic
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.
NuLogic
NuLogic
At least, how to reach that weapon object?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.