Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. ***************************MenuState***************************************************************
  2. private void select() {
  3. if(currentChoice == 0) {
  4. gsm.setState(GameStateManager.LEVEL1STATE); <------------------------- User starts game
  5. }
  6. if(currentChoice == 1) {
  7. //help
  8. }
  9. if(currentChoice == 2) {
  10. System.exit(0);
  11. }
  12. }
  13.  
  14. *********************GanePanel**********************************************************************
  15. //Called when addNotify is implicitly called abd the thread starts.
  16. public void run() {
  17.  
  18. //Calls initialize method
  19. init();
  20.  
  21. //Variables for game loop
  22. long start;
  23. long elapsed;
  24. long wait;
  25.  
  26. //game loop
  27. while(running) {
  28. start = System.nanoTime();
  29.  
  30. update(); <------------------------------------------------------------------------
  31. draw();
  32. drawToScreen();
  33.  
  34. elapsed = System.nanoTime() - start;
  35. wait = targetTime - elapsed / 1000000;
  36.  
  37. if(wait < 0) wait = 5;
  38.  
  39. try {
  40. Thread.sleep(wait);
  41. }
  42. catch(Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46.  
  47. }
  48.  
  49. //Updates game current state in array
  50. private void update() {
  51. gsm.update();<---------------------------------------------------
  52. }
  53.  
  54. *****************************************GaneStateManager************************
  55. public void update() {
  56. try {
  57. gameStates[currentState].update(); <------------------------------------ (won't work outside try/catch)
  58. } (Does work inside but throws exception)
  59. catch(Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. private GameState[] gameStates; <----------------------------------------- Variables used
  65. private int currentState; <---------------------------------------------
  66. public static final int NUMGAMESTATES = 2;<------------------------------
  67. public static final int MENUSTATE = 0;<-----------------------------------
  68. public static final int LEVEL1STATE = 1;<---------------------------------
  69.  
  70. //Constructor that creates game state
  71. public GameStateManager() {
  72.  
  73. gameStates = new GameState[NUMGAMESTATES];
  74.  
  75. //Sets current state to menu and populates gamestates
  76. currentState = MENUSTATE;
  77. loadState(currentState);
  78.  
  79. }
  80.  
  81. // Loads in correct state when needed
  82. private void loadState(int state) {
  83. if(state == MENUSTATE) {
  84. gameStates[state] = new MenuState(this);
  85. }
  86. if(state == LEVEL1STATE) {
  87. gameStates[state] = new Level1State(this);
  88. }
  89. }
  90.  
  91. // Unloads state to free memory
  92. public void unloadState(int state) {
  93. gameStates[state] = null;
  94. }
  95.  
  96. //Method for setting current state and initializes it.
  97. public void setState(int state) {
  98. unloadState(currentState);
  99. currentState = state;
  100. loadState(currentState);
  101. //gameStates[currentState].init();
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement