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_-_slickblocks
Sorry 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:
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
/**
* @author Tyler Matchett
*/
public class CodaClient extends StateBasedGame {
public static final int MAINMENUSTATE = 0;
public static final int GAMEPLAYSTATE = 1;
public static final int HIGHSCORESTATE = 2;
//public static final int OPTIONSSTATE = 3;
//public static final int LOGINSTATE = 4;
public CodaClient()
{
super("Coda - The Code Breaking Game");
this.addState(new MainMenuState(MAINMENUSTATE));
this.addState(new GameplayState(GAMEPLAYSTATE));
this.addState(new HighscoreState(HIGHSCORESTATE));
//this.addState(new OptionsState(OPTIONSSTATE));
//this.addState(new LoginState(LOGINSTATE));
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);
//this.getState(OPTIONSSTATE).init(gameContainer, this);
//this.getState(LOGINSTATE).init(gameContainer, this);
}
}
The state I start in:
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 declarations
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;
// Button effects
float playScale = 1;
float highscoreScale = 1;
float optionsScale = 1;
float exitScale = 1;
float loginScale = 1;
float scaleStep = 0.0001f;
// Menu Sound Effects
//Sound fx = null;
MainMenuState(int stateID)
{
this.stateID = stateID;
}
@Override
public int getID() {
return stateID;
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
// Load the images
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");
// Load Sounds
//fx = new Sound("res/sounds/menu_selection.wav");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
// draw the intro menu
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;
// Play button
if( ( mouseX >= 0 && mouseX <= play_button.getWidth()) && ( mouseY >= 90 && mouseY <= 90 + play_button.getHeight()) ){
insidePlay = true;
} // Highscore button
else if( ( mouseX >= 0 && mouseX <= highscore_button.getWidth()) && ( mouseY >= 196 && mouseY <= 196 + highscore_button.getHeight()) ){
insideHighscore = true;
} // Options button
else if( ( mouseX >= 0 && mouseX <= options_button.getWidth()) && ( mouseY >= 277 && mouseY <= 277 + options_button.getHeight()) ){
insideOptions = true;
} // Quit button
else if( ( mouseX >= 0 && mouseX <= quit_button.getWidth()) && ( mouseY >= 358 && mouseY <= 358 + quit_button.getHeight()) ){
insideQuit = true;
} // Login button
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) ){
//sb.enterState(CodaClient.OPTIONSSTATE);
}
}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) ){
//sb.enterState(CodaClient.LOGINSTATE);
}
}else{
if(loginScale > 1.0f)
loginScale -= scaleStep * delta;
if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
gc.exit();
}
}
}
}
The state I want to switch to:
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 declarations
Image bkg = null;
HighscoreState(int stateID)
{
this.stateID = stateID;
}
@Override
public int getID() {
return stateID;
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
// Load the images
bkg = new Image("res/bkg.jpg");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
// draw the intro menu
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();
}
}