【问题标题】:Referencing an element from Arraylist [closed]引用 Arraylist 中的元素 [关闭]
【发布时间】:2023-04-01 18:10:02
【问题描述】:

所以我的 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++;

}

【问题讨论】:

    标签: java oop arraylist 2d


    【解决方案1】:

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

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

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

    【讨论】:

    • level.player 正在返回 Null!我认为这是玩家成为数组列表或其他东西中的实体的问题......我不确定!现在我知道了有什么建议吗?
    • 他只是告诉你具体怎么做。
    • @user1837518 您提供的代码中没有 ArrayList。你能告诉我们player是如何声明和初始化的吗?
    猜你喜欢
    • 2012-04-25
    • 2012-10-17
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多