Eames
Eames

Reputation: 319

Slick2D Simple StateBasedGame not working

I recently started trying to work with outside Java libraries like Slick2D. This is my first time doing so, so I basically have no clue what I am doing.

I tried making a simple StateBasedGame that only displays a menu bar showing three options. The problem is that in my 'Game' class, the initStatesList() method isn't being called and I don't know why not. Here's the code:

Game class

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Game extends StateBasedGame {
    public Game(String title) {
        super(title);
    }

    public static void main(String[] args) {
        Game game = new Game("Text Based");
    }

    @Override
    public void initStatesList(GameContainer arg0) throws SlickException {
        System.out.println("InitStates");
        addState(new Menu());
    }
}

Menu class:

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Menu extends BasicGameState {

    private int ID = 1;

    @Override
    public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        g.setColor(Color.white);
        g.drawString("Higher or Lower", 50, 10);

        g.drawString("1. Play Game", 50, 100);
        g.drawString("2. High Scores", 50, 120);
        g.drawString("3. Quit", 50, 140);

    }

    @Override
    public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {

    }

    @Override
    public int getID() {
        return ID;
    }

}

I have no clue why it doesn't work...

Upvotes: 0

Views: 231

Answers (2)

LOLWTFasdasd asdad
LOLWTFasdasd asdad

Reputation: 2943

I think what's missing is that in your initStateList method the following is missing:

enterState(1);

So you are adding your state, but you are not entering it. In addition you need what FumbleWumble said, to really call the initStateList method.

Upvotes: 0

user3699818
user3699818

Reputation:

Sorry if this is a bit late, but here is the answer:

public static void main(String[] args) {
    Game game = new Game("Text Based");
}

This doesn't work. This is what I usually put:

    AppGameContainer appgc = new AppGameContainer(new Game("game"));
    appgc.setDisplayMode(1280, 720, false);
    appgc.setTargetFrameRate(59);
    appgc.setShowFPS(false);
    appgc.start();

So you make an AppGameContainer with a new "Game" class, then you can set the resolution and whether it is fullscreen, then the target fps, the if it should show fps, and a few other things you can do. Then you start it.

Hope this helped :)

Upvotes: 1

Related Questions