ゲームのスプラッシュ画面を作ろうとしています。無駄にGoogleを検索しました。私はこちらのチュートリアルに従っていましたが、彼にとってはうまくいきました。私のコードは少し異なっていましたが、彼のコードに少し似ていました。これが私のコードです:
ゲームクラス:
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;
}
}
私が得るエラーは次のとおりです。
Sat Apr 27 22:48:01 EST 2013 INFO: Slick Build #274
土 4 月 27 日 22:48:01 EST 2013 INFO: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:Starting display 800x600
Sat Apr 27 22:48:01 EST 2013 情報: Java PNG ローダーを使用 = true
Sat Apr 27 22:48:01 EST 2013 情報: コントローラーは利用できません
スレッド「メイン」での例外 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) で