Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>in the past I have used manually coded ints (effectively an enum).</p> <p>For example:</p> <pre><code>// "GAME" application state sub states; private int GAMESTATE; private static final int INTRO = 0; private static final int PLAYING = 1; private static final int OUTRO = 2; </code></pre> <p>then based on the value of GAMESTATE I could control the branching of the game code.</p> <p>My preferred way of creating states though is to make a state manager class which allow instanciation of a new state at need, and can control what states are active, as well as what valid state to state movement is allowed (for example allow <code>intro</code> state to <code>play</code> state but not <code>intro</code> state to victory <code>state</code>)</p> <p>(an example of something similar is located <a href="http://blog.nuclex-games.com/tutorials/cxx/game-state-management/" rel="nofollow">here</a> for C++ but it is trivial to implement the same functionality in java based on that code:)</p> <p>I have just found an old set of my code which has the following (forgive the formatting it is from about 10 years ago, and designed for minimal line count layout):</p> <p><strong>GameStateManager.java</strong></p> <pre><code>import java.awt.Image; import java.awt.Graphics2D; import java.util.*; public class GameStateManager { public static final String EXIT_GAME = "_ExitGame"; private Map gameStates; private Image defaultImage; private GameState currentState; private InputManager inputManager; private boolean done; public GameStateManager(InputManager inputManager, Image defaultImage) { this.inputManager = inputManager; this.defaultImage = defaultImage; gameStates = new HashMap(); } public void addState(GameState state) { gameStates.put(state.getName(), state); } public Iterator getStates() { return gameStates.values().iterator(); } public void loadAllResources(ResourceManager resourceManager) { Iterator i = getStates(); while (i.hasNext()) { GameState gameState = (GameState)i.next(); gameState.loadResources(resourceManager); } } public boolean isDone() { return done; } public void setState(String name) { // clean up old state if (currentState != null) { currentState.stop(); } inputManager.clearAllMaps(); if (name == EXIT_GAME) { done = true; } else { // set new state currentState = (GameState)gameStates.get(name); if (currentState != null) { currentState.start(inputManager); } } } public void update(long elapsedTime) { // if no state, pause a short time if (currentState == null) { try { Thread.sleep(100); } catch (InterruptedException ex) { } } else { String nextState = currentState.checkForStateChange(); if (nextState != null) { setState(nextState); } else { currentState.update(elapsedTime); } } } public void draw(Graphics2D g) { if (currentState != null) { currentState.draw(g); } // if no state, draw the default image to the screen else { g.drawImage(defaultImage, 0, 0, null); } } } </code></pre> <p><strong>GameState.java</strong> (Interface)</p> <pre><code>import java.awt.Graphics2D; public interface GameState { public String getName(); // Returns the name of a state to change to if this state is // ready to change to another state, or null otherwise. public String checkForStateChange(); // Loads any resources for this state. This method is called // in a background thread before any GameStates are set. public void loadResources(ResourceManager resourceManager); // Initializes this state and sets up the input manager public void start(InputManager inputManager); // Performs any actions needed to stop this state. public void stop(); // Updates world, handles input. public void update(long elapsedTime); // Draws to the screen. public void draw(Graphics2D g); } </code></pre> <p>You will also need an implementation of an <code>InputManager</code> and a <code>ResourceManager</code>, but hopefully this helps you a bit:)</p> <p>Let me know if you need more info as there are a couple of ways to do this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload