在游戏场上移动图形时 Npe(java)



我是Java的初学者,我正在尝试制作自己的非常简单的游戏"炸弹人"。

以下是我创建的类:

public class GameField {
    private Figure[][] gameField;
    public Figure[][] getGameField() {
        return gameField;
    }
    public void createField(int size) {
        this.gameField = new Figure[size][size];
        setBomberMan();
    }
    private void setBomberMan() {
        int cellX = new Random().nextInt(gameField.length);
        int cellY = new Random().nextInt(gameField.length);
        Bomberman hero = new Bomberman(cellX, cellY, "Bomberman");
        this.gameField[cellX][cellY] = hero;
    }
}

上面代码的结果如下所示:

[ ] [ ] [ ] [ ] [ ] 
[B] [ ] [ ] [ ] [ ] 
[ ] [ ] [ ] [ ] [ ] 
[ ] [ ] [ ] [ ] [ ] 
[ ] [ ] [ ] [ ] [ ]

B是我们的炸弹人,[ ] - 2D 阵列的空单元格。

图形的抽象类:

abstract class Figure {
    private int x;
    private int y;
    private String name;
    protected GameField gameField;
    protected Figure(int x, int y, String name) {
        this.x = x;
        this.y = y;
        this.name = name;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public String getFigureName() {
        return name;
    }
    abstract void move(int x, int y);
}

炸弹人 我正试图迈出一步:

public class Bomberman extends Figure {
    protected Bomberman(int x, int y, String name) {
        super(x, y, name);
    }
    @Override
    void move(int x, int y) {
        StepMaker step = new StepMaker();
        Integer[] newPosition = step.getDestination(x, y, null);

        //The problem is in the line below:
        gameField.getGameField()[x][y] = null;

        int a = newPosition[0];
        int b = newPosition[1];
        gameField.getGameField()[a][b] = new Bomberman(a, b, "Bomberman");
    }
}

类获取Integer[]坐标以执行步骤:

public class StepMaker {
    private final EnumMap<Step, Integer[]> steps = new EnumMap<>(Step.class);
    private void fillEnumMap() {
        this.steps.put(Step.UP, new Integer[]{-1, 0});
        this.steps.put(Step.DOWN, new Integer[]{1, 0});
        this.steps.put(Step.LEFT, new Integer[]{0, -1});
        this.steps.put(Step.RIGHT, new Integer[]{0, 1});
    }
    public Integer[] getDestination(int x, int y, Step step) {
        fillEnumMap();
        int destX, destY;
        Integer[] way;
        if (step != null) {
            way = this.steps.get(step);
            destX = x + way[0];
            destY = y + way[1];
        } else {
            way = this.steps.get(choose());
            destX = x + way[0];
            destY = y + way[1];
        }
        return new Integer[]{destX, destY};
    }
    private Step choose() {
        return Step.values()[ThreadLocalRandom.current().nextInt(Step.values().length)];
    }
    private enum Step {
        /**
         * Possible ways to make step.
         */
        UP, DOWN, LEFT, RIGHT;
    }
}

还有一个我运行代码的类:

public class Run {
    public static void main(String[] args) {
        GameField gameField = new GameField();
        gameField.createField(5);
        int p = 0;
        while (p != 1) {
            for (int i = 0; i < gameField.getGameField().length; i++) {
                for (int j = 0; j < gameField.getGameField()[i].length; j++) {
                    if (gameField.getGameField()[i][j] != null) {
                        gameField.getGameField()[i][j].move(i, j);
                    }
                }
            }
            gameField.printField(gameField.getGameField());
            p++;
        }
    }
}

问题是我这里有NPE(炸弹人的移动方法(:

gameField.getGameField()[x][y] = null;

在这个坐标上,我们应该有我们的炸弹人。我将此值设为空值,然后在新坐标上创建一个新图形。每个图形的 x 和 y 坐标等于我们的 2D 数组(游戏场(第一和第二坐标。但是在《奔跑》中,我说:

if (gameField.getGameField()[i][j] != null)

(这意味着我们必须有一个可以移动的图形(。

因此,该单元格不应为空,也不应引发 NPE。怎么了?

似乎gameField没有为您的BomberMan设置。您可以像这样扩展BomberMan 的构造函数和Figure的构造函数(将gameField添加为 arg(:

protected Bomberman(int x, int y, String name, Figure[][] gameField) {
    super(x, y, name, gameField);
}

和:

protected Figure(int x, int y, String name, Figure[][] gameField) {
    this.x = x;
    this.y = y;
    this.name = name;
    this.gameField = gameField;
}

然后创建一个带有额外参数的新炸弹人:

Bomberman hero = new Bomberman(cellX, cellY, "Bomberman", gameFieldValue);

最新更新