嗨ppeps(破碎的英语),
我正在制作2D迷宫游戏。我需要一些帮助来存储我的2D数组中的对象。我在这个上得到一个NullPointer:this.currentMap [colsCount] [rowsCount] = objects.get(col);. main调用LevelGenerator构造函数。
public final class LevelGenerator extends JFrame {
private HashMap<String, ItemObject> objects = new HashMap<>();
private ItemObject[][] currentMap;
private int HEIGHT = 320;
private int WIDHT = 480;
public JFrame frame = null;
public Level currentLevel = null;
private final List<Level> levels
= new ArrayList<Level>() {
{
add(new Level001());
add(new Level002());
add(new Level003());
}
};
public LevelGenerator() {
// Vul de frame
//this.frame = frame;
// Vul de objecten lijst
//objects.put("B", new Bazooka());
objects.put("", new EmptyTile());
objects.put("W", new Wall());
this.currentLevel = levels.get(0);
this.Load();
}
/// Laad de map in
public void Load() {
int rowsCount = 0;
int colsCount = 0;
for (String[] row : this.currentLevel.map) {
for (String col : row) {
this.currentMap[colsCount][rowsCount] = objects.get(col);
colsCount += 1;
}
rowsCount += 1;
}
this.Start();
}
public void Start() {
this.frame.setSize(this.HEIGHT, this.WIDHT);
this.frame.setLayout(new GridLayout(15, 10));
this.frame.add(this.currentLevel);
this.frame.setResizable(false);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setVisible(true);
}
}
itemObject代码:
public class ItemObject {
public int x = 0;
public int y = 0;
public String image = "";
public void setImage(String image) {
this.image = image;
}
}
答案 0 :(得分:1)
您尚未在任何地方初始化ItemObject[][] currentMap
。在方法start()
中,您可以添加以下代码行:
currentMap = new ItemObject[*# of rows*][*# of columns*];
在向数组添加任何值之前,必须先初始化对象。
答案 1 :(得分:0)
Oke家伙我修好了。现在我想要它在JFrame上。我无法弄明白...... W = Wall和“”= emptytile。代码现在看起来像这样:
等级:
app.use((req, res, next) => {
req.property = setProperty();
next();
});
Levelgenerator:
public class Level extends JComponent {
final int ROWS = 10;
final int COLUMNS = 15;
public String[][] map = new String[ROWS][COLUMNS];
private ItemObject[][] loadedMap;
public void Load(HashMap<String, ItemObject> objects) {
int rowsCount = 0;
for (String[] row : this.map) {
int colsCount = 0;
for (String col : row) {
this.loadedMap[rowsCount][colsCount] = objects.get(col);
colsCount += 1;
}
rowsCount += 1;
}
}
level001:
public final class LevelGenerator extends JFrame {
private HashMap<String, ItemObject> objects = new HashMap<>();
private int HEIGHT = 500;
private int WIDTH = 750;
public JFrame frame = null;
public Level currentLevel = null;
private final List<Level> levels
= new ArrayList<Level>() {
{
add(new Level001());
add(new Level002());
add(new Level003());
}
};
public LevelGenerator(JFrame frame) {
// Vul de frame
this.frame = frame;
// Vul de objecten lijst
objects.put("B", new Bazooka());
objects.put("", new EmptyTile());
objects.put("W", new Wall());
this.currentLevel = levels.get(0);
this.Load();
}
/// Laad de map in
public void Load() {
this.currentLevel.Load(objects);
this.Start();
}
public void Start() {
if (this.frame != null) {
this.frame.setSize(this.WIDTH, this.HEIGHT);
this.frame.setLayout(new GridLayout(15, 10));
this.frame.add(this.currentLevel);
this.frame.setResizable(false);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setVisible(true);
}
}
ItemObject:
public class Level001 extends Level {
public Level001() {
String[][] tiles = { { "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "", "", "", "", "", "", "", "", "", "", "", "", "", "W" },
{ "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W" }
};
super.map = tiles;
}
emptyTile对象;
public class ItemObject {
public int x = 0;
public int y = 0;
private String image = "";
private ImageIcon imageIcon;
public ImageIcon getImageIcon() {
return new ImageIcon(this.image);
}
public void setImage(String url) {
this.image = "/com/maze/images/" + url;
}
墙对象:
public class EmptyTile extends ItemObject {
public EmptyTile(){
this.setImage("Empty.png");
}