所以我的Player扩展了Mob,它扩展了Entity。在我的Level课程中,我有一个数组列表,并在主游戏课程中加入他。引用Arraylist中的元素
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++;
}
在第一level.player.x :(无论我怎样尝试引用它通过game.player.x或其他任何我可以想到
这里是主要问题:在另一个类中有一个新的“玩家”来自阵列列表如何在我的(Entites> Mob>)Sprite中引用他的x和y类?
如果上面的代码isn足够了,这里是其余的重要位:
这里是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;
}
}
这里是我的泥类的主要部分!
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++;
}
level.player正在返回Null!我认为这是一个球员是一个阵列中的实体或什么的问题......我不知道!现在有什么建议我知道吗? – user1837518
他刚刚告诉你如何做到这一点。 –
@ user1837518您提供的代码中没有ArrayList。你能告诉我们如何声明和初始化玩家吗? –