Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: Managing my Game State... suggestions?

  1. #1
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Managing my Game State... suggestions?

    Hello!

    I'm in the middle of making a game and I'm wondering what the best way was to go about game states (ie: menu, inGame, end, etc).

    What I was thinking was to have several "Board" classes for each game state and in the Main class, I would check a global variable to see what state the game was and add that class to the Main constructor.

    IE:

    Java Code:
    public class Main extends JFrame{
    
    	public Main(){
    
    		if(gameState.equals("Menu")){
    			add(new StartBoard());
    		}else if(gameState.equals("inGame")){
    			add(new GameBoard());
    		}else ...{
    		}
    
    		setTitle("My Game");
    		setSize(400, 400);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		
    		setVisible(true);
    
    	}
    
    	public static void main(String args[]){
    		new Main();
    	}
    
    }
    So, my problem now is: How do I create and use this global variable that keeps track of my game state? Should I use an enum (I don't know a lot about enums)? A public variable in my Main class?

    I'm also open to other suggestions on how I've done the above code. The reason I initially thought of doing it this way was because I didn't want to shove all of the game state code into my Board class. It seemed confusing and messy.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default Re: Managing my Game State... suggestions?

    I vote for the enum :-) (if you really need (the) states...)

  3. #3
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Managing my Game State... suggestions?

    Quote Originally Posted by eRaaaa View Post
    I vote for the enum :-) (if you really need (the) states...)
    So, how would you handle switching from the Start Menu to the Game Board to the End Menu?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default Re: Managing my Game State... suggestions?

    My controller class would do this :-)

    a) a State class (enum)
    b) a game class which has a state (reference) and a method do set/change the state
    c) a controller which switch between these states (e.g. game.setState(GameState.START)

    ?....or what do you mean?

  5. #5
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Managing my Game State... suggestions?

    Quote Originally Posted by eRaaaa View Post
    My controller class would do this :-)

    a) a State class (enum)
    b) a game class which has a state (reference) and a method do set/change the state
    c) a controller which switch between these states (e.g. game.setState(GameState.START)

    ?....or what do you mean?
    I think that's what I mean.

    Basically I'm just trying to figure out how, when the game runs it start with a menu. The user chooses their options and click the play game button -- then the game switches from drawing the menu screen to drawing the actual game.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

Similar Threads

  1. Managing my own repo using Subversive
    By kjkrum in forum Eclipse
    Replies: 3
    Last Post: 06-14-2011, 04:49 AM
  2. Replies: 2
    Last Post: 02-14-2011, 03:27 PM
  3. Question about creating a Game State Manager
    By nolsen01 in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 11:23 PM
  4. Beginning Game Development - Suggestions?
    By JDCAce in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2008, 06:42 PM
  5. Managing jPanels in jSplitPane
    By calexander in forum Advanced Java
    Replies: 6
    Last Post: 11-13-2008, 08:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •