Hey, I'm having difficulty figuring out why I cannot swap between 2 different states, the game just crashes after I click the button to make the switch. Was just wondering if someone could give me a hand?
I run the game, click on the high scores button, then the display closes. I was following the SlickBox example
http://slick.cokeandcode.com/wiki/doku.php?id=02_-_slickblocksSorry about the wall of code, but I really don't know were the issue is.
EDIT: after a bit more debugging it seems to be on the input, when I click anywhere on the screen, it just closes the display, is there anything that could be causing that?
Heres my Main file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.GameContainer; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.StateBasedGame;
public class CodaClient extends StateBasedGame { public static final int MAINMENUSTATE = 0; public static final int GAMEPLAYSTATE = 1; public static final int HIGHSCORESTATE = 2; public CodaClient() { super("Coda - The Code Breaking Game"); this.addState(new MainMenuState(MAINMENUSTATE)); this.addState(new GameplayState(GAMEPLAYSTATE)); this.addState(new HighscoreState(HIGHSCORESTATE)); this.enterState(MAINMENUSTATE); } public static void main(String[] args) throws SlickException { AppGameContainer app = new AppGameContainer(new CodaClient()); app.setDisplayMode(800, 600, false); app.start(); } @Override public void initStatesList(GameContainer gameContainer) throws SlickException { this.getState(MAINMENUSTATE).init(gameContainer, this); this.getState(GAMEPLAYSTATE).init(gameContainer, this); this.getState(HIGHSCORESTATE).init(gameContainer, this); } } |
The state I start in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.Sound; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; public class MainMenuState extends BasicGameState { int stateID = 0; Image bkg = null; Image loginbkg = null; Image logo = null; Image play_button = null; Image highscore_button = null; Image options_button = null; Image quit_button = null; Image login_button = null; float playScale = 1; float highscoreScale = 1; float optionsScale = 1; float exitScale = 1; float loginScale = 1; float scaleStep = 0.0001f; MainMenuState(int stateID) { this.stateID = stateID; } @Override public int getID() { return stateID; } public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { bkg = new Image("res/bkg.jpg"); loginbkg = new Image("res/loginbkg.jpg"); logo = new Image("res/logo.png"); play_button = new Image("res/btnPlay.png"); highscore_button = new Image("res/btnHighscore.png"); options_button = new Image("res/btnOptions.png"); quit_button = new Image("res/btnQuit.png"); login_button = new Image("res/btnLogin.png"); } public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { bkg.draw(0, 0); logo.draw(470, 90); play_button.draw(0, 90, playScale); highscore_button.draw(0, 196, highscoreScale); options_button.draw(0, 277, optionsScale); quit_button.draw(0, 358, exitScale); login_button.draw(0, 505, loginScale); g.drawString("Username [Highscore]", 570f, 510f); g.drawString("Main Menu", 50f, 50f); } public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException { Input input = gc.getInput(); int mouseX = input.getMouseX(); int mouseY = input.getMouseY(); boolean insidePlay = false; boolean insideHighscore = false; boolean insideOptions = false; boolean insideQuit = false; boolean insideLogin = false; if( ( mouseX >= 0 && mouseX <= play_button.getWidth()) && ( mouseY >= 90 && mouseY <= 90 + play_button.getHeight()) ){ insidePlay = true; } else if( ( mouseX >= 0 && mouseX <= highscore_button.getWidth()) && ( mouseY >= 196 && mouseY <= 196 + highscore_button.getHeight()) ){ insideHighscore = true; } else if( ( mouseX >= 0 && mouseX <= options_button.getWidth()) && ( mouseY >= 277 && mouseY <= 277 + options_button.getHeight()) ){ insideOptions = true; } else if( ( mouseX >= 0 && mouseX <= quit_button.getWidth()) && ( mouseY >= 358 && mouseY <= 358 + quit_button.getHeight()) ){ insideQuit = true; } else if( ( mouseX >= 0 && mouseX <= login_button.getWidth()) && ( mouseY >= 505 && mouseY <= 505 + login_button.getHeight()) ){ insideLogin = true; } if(insidePlay){ if(playScale < 1.015f) playScale += scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ sb.enterState(CodaClient.GAMEPLAYSTATE); } }else{ if(playScale > 1.0f) playScale -= scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ gc.exit(); } } if(insideHighscore){ if(highscoreScale < 1.015f) highscoreScale += scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ sb.enterState(CodaClient.HIGHSCORESTATE); } }else{ if(highscoreScale > 1.0f) highscoreScale -= scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ gc.exit(); } } if(insideOptions){ if(optionsScale < 1.015f) optionsScale += scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ } }else{ if(optionsScale > 1.0f) optionsScale -= scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ gc.exit(); } } if(insideQuit) { if(exitScale < 1.015f) exitScale += scaleStep * delta; }else{ if(exitScale > 1.0f) exitScale -= scaleStep * delta; } if(insideLogin){ if(loginScale < 1.015f) loginScale += scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ } }else{ if(loginScale > 1.0f) loginScale -= scaleStep * delta; if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){ gc.exit(); } } } } |
The state I want to switch to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; public class HighscoreState extends BasicGameState { int stateID = 2; Image bkg = null; HighscoreState(int stateID) { this.stateID = stateID; } @Override public int getID() { return stateID; } public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { bkg = new Image("res/bkg.jpg"); } public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { bkg.draw(0, 0); g.drawString("Highscores", 50f, 50f); } public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { Input input = gc.getInput(); int mouseX = input.getMouseX(); int mouseY = input.getMouseY(); } } |