引用从ArrayList中的元素 [英] Referencing an element from Arraylist

查看:122
本文介绍了引用从ArrayList中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的球员延伸暴民,它扩展实体。在我的班级我有一个ArrayList,并在主游戏类,我加了他。

 公开级水平;
     公共Player播放器;

    球员=新玩家(等级,100,100,输入,JOptionPane.showInputDialog(这一点,用户名:));
    level.addEntity(播放器);
 

在问题出现的时候,我要引用的玩家X和Y这样我就可以插入到这些我简单的煤泥怪物AI!​​

我总是得到一个NullPointerException异常这里(7号线):

 公共无效打勾(){
     INT XA = 0;
     诠释YA = 0;


    如果(randomWalkTime == 0){
        INT XD = level.player.x  -  X;
        INT YD = level.player.y  - ÿ;
        如果(XD * xd的+码*平方码50 * 50){
            XA = 0;
            雅= 0;
            如果(XD℃,)XA = -1;
            如果(XD大于0)XA = 1;
            如果(码℃,)亚= -1;
            如果(码大于0)亚= 1;
        }
    }
     移动(XA,YA);
     randomWalkTime ++;
     计时单位计数++;

}
 

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

这是最主要的问题:有一个新的玩家,也就是从另一个类的ArrayList。我如何引用他的X和Y在我(的entites>暴民>)Sprite类?

如果在code以上是不够的,这里是重要的位,其余:

下面是平级的,我有我的实体的ArrayList!

 公共类等级{

私人byte []的瓷砖;
公众诠释的宽度;
公众诠释的高度;
公开名单<实体>实体=新的ArrayList<实体>();
私人字符串的ImagePath;
私人BufferedImage的图像;
 

实体类!

 公共抽象类实体{

公众诠释的x,y;
保护级别水平;

公共实体(级别等级){
    的init(水平);
}

公众最终无效的init(级别等级){
    this.level =水平;
}


公共抽象无效打勾();

公共抽象无效渲染(屏幕画面);
 

}

暴民类:

 公共抽象类暴民扩展实体{

受保护的字符串名称;
保护INT速度;
保护INT numSteps = 0;
保护布尔isMoving;
保护INT movingDir = 1;
保护int标= 1;

公共暴民(级别水平,字符串名称,诠释的x,INT Y,INT速度){
    超(级);
    this.name =名称;
    this.x = X;
    this.y = Y;
    this.speed =速度;
}

公共无效移动(INT XA,诠释雅){
    如果(XA = 0&放大器;!&安培;!YA = 0){
        移动(XA,0);
        移动(0,雅);
        numSteps--;
        返回;
    }
    numSteps ++;
    如果(!hasCollided(XA,YA)){
        如果(亚℃,)
            movingDir = 0;
        如果(亚大于0)
            movingDir = 1;
        如果(xa的小于0)
            movingDir = 2;
        如果(xa的0)
            movingDir = 3;
        X + = XA *速度;
        Y + =雅*速度;
    }
}

公共抽象布尔hasCollided(INT XA,诠释雅);

保护布尔isSolidTile(INT XA,诠释雅,诠释的x,int y)对{
    如果(水平== NULL){
        返回false;
    }
    瓷砖lastTile = level.getTile((this.x + X)>→3,(this.y + Y)>→3);
    瓷砖newTile = level.getTile((this.x + X + XA)>→3,(this.y + Y +亚)GT;→3);
    如果(!lastTile.equals(newTile)及&安培; newTile.isSolid()){
        返回true;
    }
    返回false;
}

公共字符串的getName(){
    返回名称;
}
 

}

下面是我的泥类的主要部分!

 公共类煤泥扩展暴民{

INT XA,雅;
私人INT颜色= Colours.get(-1,111,145,543);

私人诠释randomWalkTime = 0;
私人布尔isSwimming;
私人诠释计时单位计数= 0;


公共史莱姆(级别水平,字符串名称,诠释的x,INT Y,INT速度){
    超级(水平,煤泥,X,Y,1);
    X = this.x;
    Y = this.y;


}

公共无效打勾(){
     INT XA = 0;
     诠释YA = 0;


    如果(randomWalkTime == 0){
        INT XD = level.player.x  -  X;
        INT YD = level.player.y  - ÿ;
        如果(XD * xd的+码*平方码50 * 50){
            XA = 0;
            雅= 0;
            如果(XD℃,)XA = -1;
            如果(XD大于0)XA = 1;
            如果(码℃,)亚= -1;
            如果(码大于0)亚= 1;
        }
    }
     移动(XA,YA);
     randomWalkTime ++;
     计时单位计数++;

}
 

解决方案

由于 level.player.x 给出了NPE,你有两个posibilities:eitehr 级别为空或 level.player 为空。有两种方法,以确定哪些是:

  1. preferrably,使用调试器设置断点的那一行,并设置了监视这两个变量。

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

So my Player extends Mob, which extends Entity. In my Level class I have an arraylist and in the main Game class, I add him.

     public Level level;
     public Player player;

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

The problem comes in when I want to REFERENCE the players X and Y so I can plug those into my simple Slime monster AI!

I always get a NullpointerException here (line 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++;

}

At the first level.player.x :( No matter how I try to reference it. Through game.player.x or anything else I can think of.

Here is the main question: There is a new "Player" that is from an arraylist in another class. How do I reference his x and y in my (Entites>Mob>) Sprite class?

If the code above isn't enough, here is the rest of the important bits:

Here is the LEVEL class where I have my entity 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;

The Entity class!

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

}

The Mob Class:

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

}

Here is the main part of my SLIME class!

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

}

解决方案

Since level.player.x gives a NPE, you have two posibilities: eitehr level is null or level.player is null. You have two ways to determine which it is:

  1. Preferrably, use a debugger set a breakpoint at the offending line and set a watch on these two variables.

  2. Add System.out.println() calls to print out the values of these two variables.

这篇关于引用从ArrayList中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆