我正在尝试使用 java slick 创建一个基于基本状态的游戏。我的 3 个阶段工作正常,但由于某种原因,其他 5 个阶段在我调用 get state 方法时会导致空点异常。所有状态都以相同的方式初始化并添加到游戏中,Load 游戏(工作状态之一)的实际代码与所有损坏的状态完全相同,只是减去了类名。我已经研究了一天左右,但不知道,任何帮助都会很棒。谢谢。
这是我的游戏类的代码 - 包含我的主要方法
package javagame.states;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
public class Game extends StateBasedGame{
public static final String gamename = "An OOP Adventure";
public static final int menu = 0;
public static final int play = 1;
public static final int new_game = 2;
public static final int load_game = 3;
public static final int char_select = 4;
public static final int item_found = 5;
public static final int monster_fight = 6;
public static final int inventory = 7;
public Game(String gamename){
super(gamename);
this.addState(new Menu(menu));
this.addState(new Play(play));
this.addState(new Newgame(new_game));
this.addState(new Loadgame(load_game));
this.addState(new Charselect(char_select));
this.addState(new Itemfound(item_found));
this.addState(new Monsterfight(monster_fight));
this.addState(new Inventory(inventory));
}
/*
*/
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.getState(load_game).init(gc, this);
//broken states
this.getState(new_game).init(gc, this);
this.getState(item_found).init(gc, this);
this.getState(char_select).init(gc, this);
this.getState(monster_fight).init(gc, this);
this.getState(inventory).init(gc, this);
//end broken states
this.enterState(menu);
}
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(640, 360, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
}
这是所有损坏的类的代码:
package javagame.states;
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 Newgame extends BasicGameState{
public Newgame(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
}
public int getID(){
return 3;
}
}