Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ***************************MenuState***************************************************************
- private void select() {
- if(currentChoice == 0) {
- gsm.setState(GameStateManager.LEVEL1STATE); <------------------------- User starts game
- }
- if(currentChoice == 1) {
- //help
- }
- if(currentChoice == 2) {
- System.exit(0);
- }
- }
- *********************GanePanel**********************************************************************
- //Called when addNotify is implicitly called abd the thread starts.
- public void run() {
- //Calls initialize method
- init();
- //Variables for game loop
- long start;
- long elapsed;
- long wait;
- //game loop
- while(running) {
- start = System.nanoTime();
- update(); <------------------------------------------------------------------------
- draw();
- drawToScreen();
- elapsed = System.nanoTime() - start;
- wait = targetTime - elapsed / 1000000;
- if(wait < 0) wait = 5;
- try {
- Thread.sleep(wait);
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- //Updates game current state in array
- private void update() {
- gsm.update();<---------------------------------------------------
- }
- *****************************************GaneStateManager************************
- public void update() {
- try {
- gameStates[currentState].update(); <------------------------------------ (won't work outside try/catch)
- } (Does work inside but throws exception)
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- private GameState[] gameStates; <----------------------------------------- Variables used
- private int currentState; <---------------------------------------------
- public static final int NUMGAMESTATES = 2;<------------------------------
- public static final int MENUSTATE = 0;<-----------------------------------
- public static final int LEVEL1STATE = 1;<---------------------------------
- //Constructor that creates game state
- public GameStateManager() {
- gameStates = new GameState[NUMGAMESTATES];
- //Sets current state to menu and populates gamestates
- currentState = MENUSTATE;
- loadState(currentState);
- }
- // Loads in correct state when needed
- private void loadState(int state) {
- if(state == MENUSTATE) {
- gameStates[state] = new MenuState(this);
- }
- if(state == LEVEL1STATE) {
- gameStates[state] = new Level1State(this);
- }
- }
- // Unloads state to free memory
- public void unloadState(int state) {
- gameStates[state] = null;
- }
- //Method for setting current state and initializes it.
- public void setState(int state) {
- unloadState(currentState);
- currentState = state;
- loadState(currentState);
- //gameStates[currentState].init();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement