gpt4 book ai didi

Java slick statebased game - 状态返回空点错误

转载 作者:行者123 更新时间:2023-11-30 11:36:08 25 4
gpt4 key购买 nike

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

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

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

最佳答案

code for Load game (one of the working states) is exactly the same as all the broken ones, minus the class name

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

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

公共(public)类 Newgame 扩展了 BasicGameState{ 私有(private)状态;//!!!

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

...

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

关于Java slick statebased game - 状态返回空点错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14710361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com