Npe tout en déplaçant une figure sur le terrain de jeu (java) [dupliquer]


Cette question a déjà une réponse ici:

Je suis un débutant en Java et j'essaie de faire mon propre jeu très simple "BomberMan".

Voici les classes que j'ai créées:

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

Le résultat du code ci-dessus ressemble à:

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

B est notre Bomberman, et [ ] - cellules vides du tableau 2D.

Classe Abstraite pour les chiffres:

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

Le Bomberman J'essaie de faire un pas:

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

Classe pour obtenir Integer[] coordonner pour faire une étape:

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

Et une classe où j'exécute mon code:

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

Le problème est que j'ai NPE ici (méthode de déplacement de Bomberman):

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

Sur cette coordonnée, nous devrions avoir notre Bomberman. Je rends cette valeur nulle, puis je crée une nouvelle figure sur une nouvelle coordonnée. Chaque figure a des coordonnées x et y égales à notre tableau 2D (champ de jeu) première et deuxième coordonnées. Mais dans Run je dis:

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

(Cela signifie que nous devons avoir une figure là-bas qui peut se déplacer).

Donc cette cellule ne doit pas être nulle et ne doit pas lancer de NPE. Quel est le problème?

Author: aLittleMind, 2017-04-10

1 answers

, Il semble que gameField n'est pas défini pour votre BomberMan. Vous pouvez étendre les constructeurs BomberMan et Figure (ajouter gameField en tant qu'arg) comme ceci:

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

Et:

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

Puis créez un nouveau BomberMan avec un arg supplémentaire:

Bomberman hero = new Bomberman(cellX, cellY, "Bomberman", gameFieldValue);
 2
Author: Anton Hlinisty, 2017-04-10 14:35:25