-1

したがって、私の Player は Mob を拡張し、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 を参照して、それらを単純なスライム モンスター 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.xNPE を与えるので、2 つの可能性があります: いずれlevelかが null またはlevel.playernull です。どちらであるかを判断するには、次の 2 つの方法があります。

  1. デバッガーを使用して問題のある行にブレークポイントを設定し、これら 2 つの変数に監視を設定することをお勧めします。

  2. System.out.println()これら 2 つの変数の値を出力する呼び出しを追加します。

于 2012-12-15T19:12:57.183 に答える