They seem to work fine. I had the main class extend StateBasedTest and had 3 other classes extend TestState1, 2 and 3, added the 3 states to the StateBasedTest, and the window that came up had a state with only text saying "State 1" and "Numbers 1 - 3 will change states".
State 2 had "State 2" and a rotating logo that said "Slick"
State 3 had "State 3" and a menu.
The only problem I found was that there seems to be no transition from State1 to State 2.
When it switches states, you can still see everything from State 1 being overlapped by State 2 for about 2-3 seconds before it disappears and the logo starts spinning. Is this a different kind of transition, or just a bug? lol
In any case, I don't know what I possibly could have done to actually
disable the init() call from being made to a state. I mean... the init() method should be called as soon as a state is added to the list, without having to make an explicit call, shouldn't it?
Is there any line of code off the top of your head that disables automatic initialization?
Here's the code for the main class and the MainMenuGameState class:
Code:
public class SlickOutGame extends StateBasedGame {
public static final int MAINMENUSTATE = 0;
public static final int GAMEPLAYSTATE = 1;
public static final int LEVELSELECTSTATE = 2;
public SlickOutGame() {
super("SlickOut");
}
@Override
public void initStatesList(GameContainer gc) throws SlickException {
GameInfo.createNewGameInfo();
this.addState(new MainMenuGameState());
//this.getState(MAINMENUSTATE).init(gc, this);
GameplayState state = new GameplayState();
this.addState(state);
this.addState(new LevelSelector());
}
public static void main(String[] args) throws SlickException {
AppGameContainer app = new AppGameContainer(new SlickOutGame());
app.setDisplayMode(800, 600, false);
app.start();
}
}
Code:
public class MainMenuGameState extends BasicGameState implements MouseListener {
private Image background;
private Image selector;
private int selection;
private int optionSelected;
private int topScore;
@Override
public int getID() {
return 0;
}
@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
background = new Image("data/mainmenu.jpg");
selector = new Image("data/selector.png");
}
@Override
public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
selection = -1;
optionSelected = selection;
if ( GameInfo.getCurrentGameInfo() != null) {
topScore = (topScore > GameInfo.getCurrentGameInfo().getPlayerInfo().getScore() ) ? topScore : GameInfo.getCurrentGameInfo().getPlayerInfo().getScore();
}
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
background.draw();
if(selection == 1) {
selector.draw(158, 310);
selector.draw(694, 310);
GameInfo.createNewGameInfo();
}else if(selection == 2) {
selector.draw(158, 474);
selector.draw(694, 474);
}
// TODO: Log this
g.drawString("TOPSCORE: " + topScore, 10, 10);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
if(optionSelected == 1) {
sbg.enterState(2);
}else if(optionSelected == 2) {
System.exit(0);
}
}
public void mouseMoved(int oldx, int oldy, int newX, int newY) {
if(newX > 228 && newX < 702) {
// start game
if (newY > 308 && newY < 389) {
selection = 1;
// exit game
}else if (newY > 475 && newY < 544) {
selection = 2;
}else {
selection = -1;
}
}
}
public void mouseClicked(int button, int x, int y, int clickCount) {
optionSelected = selection;
}
}