mercredi 18 janvier 2017

"Ghost instances" in Java?

I have a problem working with instances of different objects and this is what happens:

I have been developing a small game in Java (Swing & AWT) for a while and I have the following classes:

  • App.java
  • Play.java
  • Event.java
  • GameScene.java
  • MenuScene.java
  • Timer.java

Where:

  • App extends JFrame and is a frame with the main function of the application (main), this class creates the game window, and only exists this JFrame

  • The MenuScene and GameScene classes are scenes of the application, for example when you see the menu and you want to see the highest score, it is a scene, the levels of game are a scene, etc., but in this case I have only two scenes and I have represented them in JPanels: MenuScene extends JPanel and creates the game menu (buttons, images, etc.), the same applies to the GameScene class, this also extends JPanel and creates the game.

  • The other classes (Play, Event, Timer) are simple classes, they have the "logic of the game", keyboard control, timers, game operation and are instantiated in three global variables of the GameScene class.

Everything starts with App, creates an instance of it and in its constructor calls a method to "create" the menu (MenuScene.java). Now the menu has a JButton that when pressed "creates" the game (GameScene.java) and this class has a JButton to return to the menu at any time ... It is here where I have problems because if I am playing and I return to the menu Game still exists and I can lose, it does not make sense, it is as if you play but instead of seeing the game you see the menu, interestingly the graphic part works excellent, ie if I press a button it removes what I have and draws the scene that I want it quickly. It is because Play, Timer and Event are instantiated or "exist" in memory if I am not mistaken. So if I press again the "create game" JButton I would recreate a second instance of GameScene? And so infinitely for MenuScene and GameScene. Is there a solution to this? How do you think I should structure the application?

I give you an outline of the most important classes:

App.java

public class App extends JFrame {
   private JPanel rootPanel;

   public App() {
      //Define frame
      ...
      runScene(new MenuScene(this));
   }

   public void runScene(JPanel scene) {
      destroyScene();

      rootPanel.setBackground(scene.getBackground());
      rootPanel.add(scene);
      rootPane.validate();
      rootPanel.repaint();
   }

   private void destroyScene() {
      rootPanel.removeAll();
      rootPanel.revalidate();
      rootPanel.repaint();
   }


   public static void main(String[] args) { //Main
      new App();
   }
}

MenuScene.java

public class MenuScene extends JPanel {
   private App app;

   public MenuScene(App app) {
      this.app = app;
      //Define JPanel
      ...
      buttonStart.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            app.runScene(new GameScene(app));
        }
      });
   }
}

GameScene.java

public class GameScene extends JPanel {
   private App;
   private Play;
   private Timer;
   private Event; //Define controls (keyboard)

   public GameScene(App app) {
      this.app = app;
      //Define JPanel, Play, Timer and Event
      ...
      buttonBackMenu.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            app.runScene(new MenuScene(app));
        }
      });
   }
}

Play.java

public class Play {
   private JLabel[][] x;

   public Play(JLabel[][] x) { //This matrix is important (is an reference), is instanced in GameScene, this is an problem?
      this.x = x;
      //Define others variables
   }
}

I appreciate any help.

Aucun commentaire:

Enregistrer un commentaire