引用Arraylist中的元素

时间:2012-12-15 19:08:39

标签: java oop arraylist 2d

所以我的播放器扩展了Mob,扩展了Entity。在我的Level课程中,我有一个arraylist,在主要的Game类中,我添加了他。

     public Level level;
     public Player player;

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

当我想参考玩家X和Y时问题就出现了,所以我可以将它们插入我简单的Slime怪物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或我能想到的任何其他内容。

以下是主要问题:有一个新的“播放器”来自另一个类的arraylist。如何在我的(Entites&gt; Mob&gt;)Sprite类中引用他的x和y?

如果上面的代码不够,那么剩下的重要部分就是:

这是LEVEL类,我有我的实体arraylist!

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

}

1 个答案:

答案 0 :(得分:2)

由于level.player.x给出了一个NPE,你有两个可能性:eitehr level为空或level.player为空。您有两种方法可以确定它是什么:

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

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