-1

所以我的 Player 扩展了 Mob,后者扩展了 Entity。在我的 Level 类中,我有一个数组列表,在主 Game 类中,我添加了他。

     public Level level;
     public Player player;

    player = new Player(level, 100, 100, input, JOptionPane.showInputDialog(this, "Username:"));
    level.addEntity(player);

当我想引用玩家 X 和 Y 时问题就来了,这样我就可以将它们插入我的简单史莱姆怪物 AI!

我总是在这里得到一个 NullpointerException (第 7 行):

public void tick() {
     int xa = 0;
     int ya = 0;


    if (randomWalkTime == 0) {
        int xd = level.player.x - x;
        int yd = level.player.y - y;
        if (xd * xd + yd * yd < 50 * 50) {
            xa = 0;
            ya = 0;
            if (xd < 0) xa = -1;
            if (xd > 0) xa = +1;
            if (yd < 0) ya = -1;
            if (yd > 0) ya = +1;
        }
    }
     move(xa, ya);
     randomWalkTime++;
     tickCount++;

}

在第一级.player.x :(无论我如何尝试引用它。通过game.player.x或我能想到的任何其他东西。

这是主要问题:有一个新的“播放器”来自另一个类的数组列表。如何在我的 (Entites>Mob>) Sprite 类中引用他的 x 和 y?

如果上面的代码还不够,这里是其余的重要部分:

这是我的实体数组列表的 LEVEL 类!

public class Level {

private byte[] tiles;
public int width;
public int height;
public List<Entity> entities = new ArrayList<Entity>();
private String imagePath;
private BufferedImage image;

实体类!

public abstract class Entity {

public int x, y;
protected Level level;

public Entity(Level level) {
    init(level);
}

public final void init(Level level) {
    this.level = level;
}


public abstract void tick();

public abstract void render(Screen screen);

}

黑帮类:

public abstract class Mob extends Entity {

protected String name;
protected int speed;
protected int numSteps = 0;
protected boolean isMoving;
protected int movingDir = 1;
protected int scale = 1;

public Mob(Level level, String name, int x, int y, int speed) {
    super(level);
    this.name = name;
    this.x = x;
    this.y = y;
    this.speed = speed;
}

public void move(int xa, int ya) {
    if (xa != 0 && ya != 0) {
        move(xa, 0);
        move(0, ya);
        numSteps--;
        return;
    }
    numSteps++;
    if (!hasCollided(xa, ya)) {
        if (ya < 0)
            movingDir = 0;
        if (ya > 0)
            movingDir = 1;
        if (xa < 0)
            movingDir = 2;
        if (xa > 0)
            movingDir = 3;
        x += xa * speed;
        y += ya * speed;
    }
}

public abstract boolean hasCollided(int xa, int ya);

protected boolean isSolidTile(int xa, int ya, int x, int y) {
    if (level == null) {
        return false;
    }
    Tile lastTile = level.getTile((this.x + x) >> 3, (this.y + y) >> 3);
    Tile newTile = level.getTile((this.x + x + xa) >> 3, (this.y + y + ya) >> 3);
    if (!lastTile.equals(newTile) && newTile.isSolid()) {
        return true;
    }
    return false;
}

public String getName() {
    return name;
}

}

这是我的 SLIME 课程的主要部分!

public class Slime extends Mob{

int xa, ya;
private int colour = Colours.get(-1, 111, 145, 543);

private int randomWalkTime = 0;
private boolean isSwimming;
private int tickCount = 0;


public Slime(Level level, String name, int x, int y, int speed) {
    super(level, "Slime", x, y, 1);
    x = this.x;
    y = this.y;


}

public void tick() {
     int xa = 0;
     int ya = 0;


    if (randomWalkTime == 0) {
        int xd = level.player.x - x;
        int yd = level.player.y - y;
        if (xd * xd + yd * yd < 50 * 50) {
            xa = 0;
            ya = 0;
            if (xd < 0) xa = -1;
            if (xd > 0) xa = +1;
            if (yd < 0) ya = -1;
            if (yd > 0) ya = +1;
        }
    }
     move(xa, ya);
     randomWalkTime++;
     tickCount++;

}
4

1 回答 1

2

由于level.player.x给出了 NPE,因此您有两种可能性:eitehrlevel为空或level.player为空。您有两种方法可以确定它是哪个:

  1. 最好使用调试器在有问题的行设置断点并监视这两个变量。

  2. 添加System.out.println()调用以打印出这两个变量的值。

于 2012-12-15T19:12:57.183 回答