2

我正在尝试使用 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;
    }
 }
4

1 回答 1

1

加载游戏(工作状态之一)的代码与所有损坏的代码完全相同,减去类名

所有损坏的状态类和 Loadgame 类返回getID() == 3。仅在Game构造函数Loadgame中注册为状态id == 3- 其他人被忽略,因此从未注册。然后在initStatesList()其他状态类中找不到,因为没有使用 .id 之类的 id 注册的状态4, 5, 6, 7

修复所有损坏的状态类(包括 Loadgame),如下所示:

公共类 Newgame 扩展 BasicGameState{ 私有 int 状态;// !!!

public Newgame(int state){
    this.state = state; // !!!
}

...

public int getID(){
   return this.state; // !!!
}

}

于 2013-02-05T18:17:03.763 回答