Jump to content

  • Log In with Google      Sign In   
  • Create Account

Creating object in external class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 April 2012 - 08:02 AM

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.

Sponsor:

#2 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 April 2012 - 01:15 PM

At least, how to reach that weapon object?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS