Search code examples
javaslick2d

I cannot solve this with slick2D Java


I'm trying to make a splash screen for a game. I've searched Google without avail. I was following this tutorial here and it worked for him. My code was a little different but looked a little like his. Here is my code:

GAME CLASS:

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame {

public static final String gamename = "Life - Alpha";
public static int menu = 1;
public static int play = 2;
public static int splash = 0;

public Game(String gamename) {
    super(gamename);
}

public void initStatesList(GameContainer gc) throws SlickException {
    this.getState(menu).init(gc, this);
    this.getState(play).init(gc, this);
    this.enterState(0);
}

public static void main(String[] args) {

    AppGameContainer app;
    try {
        app = new AppGameContainer(new Game(gamename));
        app.setDisplayMode(800, 600, false);
        app.start();
    } catch (SlickException e) {
        e.printStackTrace();
    }

   }
}

MENU CLASS:

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState {

public Menu(int state) {

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

    Image back = new Image("res/Back.png");
    g.drawImage(back, 0, 0);

    Image panel = new Image("res/Panel.png");
    g.drawImage(panel, 240, 100);

    Image logo = new Image("res/Life Alpha.png");
    g.drawImage(logo, 290, 130);

    Image playButton = new Image("res/Play.png");
    g.drawImage(playButton, 330, 310);

    Image exitButton = new Image("res/Exit.png");
    g.drawImage(exitButton, 330, 370);

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {     

    Input input = gc.getInput();
    int xpos = Mouse.getX();
    int ypos = Mouse.getY();

    if((xpos > 330 && xpos < 480) && (ypos > 250 && ypos < 290)) {
        if(input.isMousePressed(0)) {
            sbg.enterState(2);
        }
    }
}

public int getID() {
    return 0;
}

}

PLAY CLASS:

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState {

public Play(int state) {

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

    Image back = new Image("res/Back.png");
    g.drawImage(back, 0, 0);

    Image panel = new Image("res/Panel2.png");
    g.drawImage(panel, 170, 100);

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

}

public int getID() {
    return 1;
}

}

SPLASHSCREEN CLASS:

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

public class SplashScreen extends BasicGameState {

Image splash;

private int elapsedTime;
private final int DELAY = 3000;

public SplashScreen(int state) {

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

    splash = new Image("res/SplashScreen.png");

}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

    g.drawImage(splash, 0, 0);

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

    elapsedTime += delta;

    if(elapsedTime >= DELAY) {

        sbg.enterState(1);

    }

}

public int getID() {
    return 0;
}

}

The error i get is:

Sat Apr 27 22:48:01 EST 2013 INFO:Slick Build #274

Sat Apr 27 22:48:01 EST 2013 INFO:LWJGL Version: 2.8.5

Sat Apr 27 22:48:01 EST 2013 INFO:OriginalDisplayMode: 1366 x 768 x 32 @60Hz

Sat Apr 27 22:48:01 EST 2013 INFO:TargetDisplayMode: 800 x 600 x 0 @0Hz

Sat Apr 27 22:48:01 EST 2013 INFO:Starting display 800x600

Sat Apr 27 22:48:01 EST 2013 INFO:Use Java PNG Loader = true

Sat Apr 27 22:48:01 EST 2013 INFO:Controllers not available

Exception in thread "main" java.lang.NullPointerException

at mms001.Game.initStatesList(Game.java:18)

at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)

at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)

at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)

at mms001.Game.main(Game.java:29)


Solution

  • It looks like there might be a couple of issues here.

    I noticed that your Menu class is returning 0 as it's ID and so is the SplashScreen class. I bet if you changed the getID of the Menu class to be 1, that would fix part of your problem.

    class Menu...
    public int getID() {
        return 1;
    }
    

    You will also need to update your Play class's getID method as well to return 2.

    class Play...
    public int getID() {
        return 2;
    }
    

    Once you do that they will match your static fields defined in your main class.

    Then update your initStatesList method to init splash and enter the menu state

    public void initStatesList(GameContainer gc) throws SlickException {
        this.getState(splash).init(gc, this);
        this.getState(menu).init(gc, this);
        this.getState(play).init(gc, this);
    
        this.enterState(menu);
    }
    

    Hope this helps.