我正在尝试为游戏制作启动画面。我搜索了谷歌没有用。我在这里关注本教程,它对他有用。我的代码有点不同,但看起来有点像他的。这是我的代码:
游戏类:
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;
}
}
闪屏类:
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;
}
}
我得到的错误是:
2013 年 4 月 27 日星期六 22:48:01 EST 信息:Slick Build #274
2013 年 4 月 27 日星期六 22:48:01 EST 信息:LWJGL 版本:2.8.5
2013 年 4 月 27 日星期六 22:48:01 EST 信息:原始显示模式:1366 x 768 x 32 @60Hz
2013 年 4 月 27 日星期六 22:48:01 EST 信息:目标显示模式:800 x 600 x 0 @0Hz
2013 年 4 月 27 日星期六 22:48:01 EST 信息:开始显示 800x600
2013 年 4 月 27 日星期六 22:48:01 EST 信息:使用 Java PNG Loader = true
2013 年 4 月 27 日星期六 22:48:01 EST 信息:控制器不可用
线程“主”java.lang.NullPointerException 中的异常
在 mms001.Game.initStatesList(Game.java:18)
在 org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
在 org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
在 org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
在 mms001.Game.main(Game.java:29)