3

Im new to Java and this site.

Im trying to develop a a game as modular as possible. So I have a GameEngine class and a GUI clase, which are completely independent. And an other class that is the Interface of both.

The problem is that I can't get the GUI Object (where the actionPreformed routine reside) to trigger methods in the GameEngine class. I'm trying to do all the programming in the interface not having to modify any of the other two classes (or at least not make them codependent).

My idea is to have the actionPerformed return a code which is processed in the interface that directs the next action.

As an example, the NewGame button generates a NewGameCode. But how can I trigger the NewGame method in the GameEngine with that code and within the Interface?

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
McMoss
  • 31
  • 1
  • 1
    post your classes/interface please – GregH Aug 10 '15 at 21:47
  • As a general rule, don't try to write the framework before you use it. It's a good idea for your game engine not to be dependent on the UI of course but start with code that invokes your engine directly from the UI and if it becomes too complicated, then you can abstract it out. – biziclop Aug 10 '15 at 21:49
  • Welcome to StackOverflow! Please be sure to read our [ask] page to help you formulate a great question. You are much more likely to get a good answer from the community if you put some effort into your question. – blurfus Aug 10 '15 at 21:52
  • Suggestions: 1) Look up [Model-View-Controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) design pattern as a good way to separate concerns and 2) [Tracer Bullet Prototype Programming](http://www.artima.com/intv/tracer.html), a concept well covered in "The The Pragmatic Programmer" by Hunt and Thomas, as a more pragmatic way to develop a complex program. – Hovercraft Full Of Eels Aug 10 '15 at 22:07
  • You said you _can' t_ access the GUI object... Why not? What can you do with the GUI? Can you attach actionListeners? If yes, you could run the interface (=controller), which will instance the engine on one hand, and the GUI on the other, programming the actionListeners needed. – Little Santi Aug 10 '15 at 22:17
  • Thanks for the answers and links. – McMoss Aug 11 '15 at 22:54
  • At this moment it seems it's better to move the actionPerforme routine to my interface. – McMoss Aug 11 '15 at 22:58

1 Answers1

0

Use a bit of programming patterns, like delegating:

ActionListener.java:

interface ActionListener {

    public boolean performAction(Action action);

}

GUI.java:

class GUI implements ActionListener {
    protected ActionListener actionListener;

    public void setActionListener(ActionListener listener) {
        this.listener = listener;
    }

    public boolean performAction(Action action) {
      // GUI knows, how to handle specified action:
      if (action.is("toggle-gui")) {
         ...
         return true;
      }

      // else, should Engine try to perform that action?
      if (listener != null)
        if (listener.performAction(action))
          return true;

      // rare occasion
      return false;
    }

}

GameEngine.java:

class GameEngine implements ActionListener {

    public boolean performAction(Action action) {
      if (action.is("game-new")) {
         // start a new game here
         ...
         return true;
      }

      return false;
    }

}

SomeInitializationCode.java

void init() {
  ...

  engine = new GameEngine();
  gui = new GUI();

  gui.setActionListener(engine);

  ...
}

Also, as you may already guessed, gui objects (like buttons) also has their action listeners. When GUI instantiates a button, it sets itself as button's action listener, so, button's action performing is delegated to GUI, and when GUI doesn't know, how to perform that specified action - it delegates work to own delegate, thus, to GameEngine.

P.S. Sorry for my weak english.

ankhzet
  • 2,517
  • 1
  • 24
  • 31