Java光滑的基于状态的游戏 - 状态返回零点错误

时间:2013-02-05 14:54:40

标签: java slick2d states

我正在尝试使用java slick来创建基于状态的基本游戏。我的3个阶段工作正常,但由于某种原因,当我调用get state方法时,其他5个引起零点异常。所有状态都以相同的方式初始化并添加到游戏中,加载游戏(其中一个工作状态)的实际代码与所有已破坏的代码完全相同,减去类名。我已经看了一天左右,并且不知道,任何帮助都会很棒。感谢。

这是我的游戏类的代码 - 包含我的主要方法

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;
    }
 }

1 个答案:

答案 0 :(得分:1)

  

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

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

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

公共类Newgame扩展BasicGameState {     私人国家; // !!!

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

...

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

}