我正试图为游戏制作启动画面。我搜索谷歌无济于事。我正在学习本教程here,这对他有用。我的代码有点不同,但看起来有点像他的。这是我的代码:
游戏类:
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();
}
}
}
菜单类:
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;
}
}
播放课程
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;
}
}
我得到的错误是:
Sat Apr 27 22:48:01 EST 2013 INFO:Slick Build#274
Sat Apr 27 22:48:01 EST 2013资料:LWJGL版本: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:开始显示800x600
Sat Apr 27 22:48:01 EST 2013 INFO:使用Java PNG Loader = true
Sat Apr 27 22:48:01 EST 2013 INFO:控制器不可用
线程“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)
答案 0 :(得分:3)
看起来这里可能存在一些问题。
我注意到你的Menu
类返回0作为它的ID,SplashScreen
类也是如此。我敢打赌,如果你将getID
类的Menu
更改为1,那将解决部分问题。
class Menu...
public int getID() {
return 1;
}
您还需要更新Play类的getID方法以返回2。
class Play...
public int getID() {
return 2;
}
一旦你这样做,他们将匹配你的主类中定义的静态字段。
然后将您的initStatesList
方法更新为初始splash
并输入menu
州
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);
}
希望这有帮助。